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;
}
}