So I am having this problem where I check if a boolean is true, and then I do audio.Play();
Problem here is that it does not play when the boolean is true, nor when a key is pressed as it’s supposed to. It plays when either key is not pressed or when the boolean is not true…
So basically, here I’m telling it that when I press a key, and player is grounded, then basically play the sound. What it does is that it completely ignores that, and it plays when GetKeyUp… Please help me with this!
Note: Yes, I am sure that the code is woking. The booleans are changing properly etc. but audio is the only part which bugs. Please help me out with this!
You restart playing the sound every single frame while the key is down, so the sound never actually plays through, it’s only when you stop pressing the key (and stop restarting the sound) that the sound can play through to the end.
If the sound is already playing, you shouldn’t play it. and if you’re not grounded then you should stop the audio.
Sadly, this does not work. I tried with booleans too but they don’t seem to be working netiher. Only when I do the literal opposite of my actions it works…
Oh, so this would work?
Right now I am trying to fix that when player is !Grounded then the audio stops playing but when he is back on ground after jumping for example, enable the audio again but it doesn ot work.
What NA-RA-KU said is true. Audio.Play will perform correctly when fired once, not every frame. You are essentially getting Unity to replay the audio each frame as long as you hold the D button.
What I will do is move the boolean Running = true; to anywhere outside of the if(Input.GetKey) loop (so the boolean will only be called once), then tie the audio play function to that boolean.