Forum begins after the advertisement:
[General] Create Vacuum PickUp
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [General] Create Vacuum PickUp
- This topic has 2 replies, 3 voices, and was last updated 2 months, 1 week ago by 정춤고 (ChoomGo).
Viewing 3 posts - 1 through 3 (of 3 total)
-
AuthorPosts
-
October 15, 2024 at 3:06 am #16076::
I tried to create the vaccum orb that collect all xp gem in the map, it temporally worked just as intended but there is a small issue. If the xp gems are too far away from the player and the life span of their prefab are short (under 5s), they will disappear before touching player (player still get the correct amount of xp, iirc), life span around 10s would do the job. VacuumPickUp.cs
<code>using System.Collections; using System.Collections.Generic; using UnityEngine; public class VaccumPickUp : MonoBehaviour { public float pullSpeed = 10f; // Speed at which the gems move toward the player private PlayerStats playerStats; // Reference to the player's stats Vector2 initialPosition; float initialOffset; // To represent the bobbing animation of the object. [System.Serializable] public struct BobbingAnimation { public float frequency; public Vector2 direction; } public BobbingAnimation bobbingAnimation = new BobbingAnimation { frequency = 2f, direction = new Vector2(0, 0.3f) }; void Start() { initialPosition = transform.position; initialOffset = Random.Range(0, bobbingAnimation.frequency); // Find the player stats (assuming PlayerStats script is attached to the player) GameObject player = GameObject.FindGameObjectWithTag("Player"); if (player != null) { playerStats = player.GetComponent<PlayerStats>(); } else { Debug.LogError("Player object not found. Make sure it has the 'Player' tag."); } } void Update() { // Apply bobbing animation even when not collecting XP transform.position = initialPosition + bobbingAnimation.direction * Mathf.Sin((Time.time + initialOffset) * bobbingAnimation.frequency); } void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { // Start collecting all XP gems on the map CollectAllXP(); // Optionally destroy the vacuum pickup object after use Destroy(gameObject); } } void CollectAllXP() { // Find all XP gems in the scene by their "XP" tag GameObject[] xpGems = GameObject.FindGameObjectsWithTag("XP Berry"); // Loop through all the XP gems and collect them foreach (GameObject xpGem in xpGems) { Pickup pickupScript = xpGem.GetComponent<Pickup>(); if (pickupScript != null) { // Call the Collect method to have the XP gem fly towards the player pickupScript.Collect(playerStats, pullSpeed); } } } } </code>
View post on imgur.com
October 15, 2024 at 5:24 pm #16083::You don’t have to increase the lifespan of the Pickup. You can add a 3rd argument to the
Collect()
function to overwrite the default lifespan:void CollectAllXP() { // Find all XP gems in the scene by their "XP" tag GameObject[] xpGems = GameObject.FindGameObjectsWithTag("XP Berry"); // Loop through all the XP gems and collect them foreach (GameObject xpGem in xpGems) { Pickup pickupScript = xpGem.GetComponent<Pickup>(); if (pickupScript != null) { // Call the Collect method to have the XP gem fly towards the player pickupScript.Collect(playerStats, pullSpeed, 10); } } }
October 17, 2024 at 1:13 am #16096 -
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.
Advertisement below: