Home > OS >  Particle system unity not always showing, unity
Particle system unity not always showing, unity

Time:06-15

So I'm trying to add a particle effect to a little space game of mine, I get axis "Vertical", then check if it is greater than 0 the particle system plays (going forward)

flyfloat = Input.GetAxis("Vertical");
if(flyfloat > 0)
{
     particles.Play();
}
else
{
     particles.Stop();
}

That controls whether it is playing the particle system, but the issue i have is that it only gives some particles and then stops, I've viewed the flyfloat and it is at 1.

What may the problem be here?

Thanks

CodePudding user response:

You question is incomplete as for example I don't know where you are using these lines of code.. inside an Update() method or a Start() method.

Assuming you are calling it in Update() method. Let me explain first what is happening wrong here. So as Update() gets called each frame when you pressing UP_Arrow key flyfloat = 1 . that's ok but now as you go inside the if loop to check flyfloat > 0 and calls partciles.Play() it is being called every Update() loop means every frame so what's happening is your ParticleSystem is getting played every frame so not playing at all. Also whenever you stops pressing the UP_Arrow key the flyfloat = 0 for which it's going inside the else loop and stops playing the ParticleSystem.

So to solve this you can introduce a Boolean which makes partciles.Play() and partciles.Stop() gets called once when you are pressing UP_Arrow key.

below code will make the ParticleSystem play when you press UP_Arrow key and stops it when you press DOWN_Arrow key

    public ParticleSystem particles;
    public float flyfloat;

    bool isParticlePlaying = false;


    private void Update()
    {
        flyfloat = Input.GetAxis("Vertical");

        if (flyfloat > 0  && !isParticlePlaying)
        {
            particles.Play();
            isParticlePlaying = true;
        }
        else if (flyfloat < 0 && isParticlePlaying)
        {
            particles.Stop();
            isParticlePlaying = false;
        }
    }
  • Related