How do you stop emitting particulates after a few seconds?

Asked

Viewed 55 times

2

I have a simple code that spawnes particles when the player is stationary on Rigger. But they don’t stop broadcasting and I’d like them to stop right after a few seconds. How can I do that?

if (other.gameObject.tag == "Player" && Input.GetKeyDown(KeyCode.E))
        {
            PlayerManager.health += 1;
            MyParticleEffect.SetActive(true);

        yield WaitForSeconds(5); // wait for 5 seconds
        MyParticleEffect.SetActive(false); // turn the particle system on

            Debug.Log("e key was pressed");
        }

1 answer

0


First of all, I think "Yield" doesn’t work on a void, it works on a ienumerator. I believe you are using Ontriggerstay. For this, the easy way would be to create a variable that counts time and use Update().

public class scriptest : MonoBehaviour {
   float timer = 0; //variável do tempo.
   public GameObject MyParticleEffect;//GameObject do Efeito

 void Update()
 {
    timer += Time.deltaTime; //Somando o timeDelta, conta-se o tempo.
    if (timer > 5) //Se o tempo for maior do que 5, o GameObject do efeito é desativado.
     {
        MyParticleEffect.SetActive(false);
     }
 }

 void OnTriggerStay(Collider other)
 {
     if (other.gameObject.tag == "Player" && Input.GetKeyDown(KeyCode.E))
     {
        //Aqui vai o PlayerManager.health += 1.
        MyParticleEffect.SetActive(true); //GameObject do efeito é ativado.
        timer = 0; //Tempo é zerado.
     }
 }
}

See also, if you don’t have any script preventing the Effect from being disabled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.