Forum begins after the advertisement:


[General] Create Vacuum PickUp

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #16076
    Kai
    Level 13
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    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
    #16083
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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);
                }
            }
        }
    #16096
    정춤고 (ChoomGo)
    Level 13
    Former Patron
    Helpful?
    Up
    1
    ::

    Wow…Amazing!

    has upvoted this post.
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: