How to stop emitting particles after a few seconds?

2

I have a simple code that spawns particles when the player gets stopped at the trigger. But they do not stop being issued and I would like them to stop after a few seconds. How can I do this?

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");
        }
    
asked by anonymous 19.09.2018 / 04:29

1 answer

0

First, I think the yield does not work on a Void, but on an IEnumerator. I think you're using OnTriggerStay. For this, the easy way would be to create a variable that counts the 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 do not have any scripts preventing the Effect from being disabled.

    
04.10.2018 / 01:45