Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I set a lifespan for a projectile?

public class ProjectileFlightTimeUntilDecay : MonoBehaviour
{
    public float PlayerProjectile;
    public float timeUntilDecay = 5.0f;

    void Start()
    {
        timeUntilDecay = timeUntilDecay * Time.deltaTime;
    }

    // Update is called once per frame
    void Update()
    {
        if(timeUntilDecay <= 0)
        {
            // Destroy the projectile
        }
    }
}

I tried this, and doesn’t work.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

timeUntilDecay is never decreased from its initial value, so the check if (timeUntilDecay <= 0) will never be true.

Typically this is done by decrementing it in Update(), e.g.

timeUntilDecay -= Time.deltaTime;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading