Audio keeps playing over and over while holding space even after fix

ive followed the instructions for this lecture up until we try to fix a bug where continually holding space causes the audio to start playing again and again making a grinding noise, but whenever i fly around it still does the audio playing over itself bug, and im not sure why?

So with this solution, in every frame that user presses space you are checking if the audio is playing, and if its is not, you play it. Than, next frame you check again and if its playing - you stop it. So on consequent frame you check again, and because its not playing (it was stopped on previous frame) you start it. As a result, you have the same audial effect as before, but instead of starting the audio file each frame, you start it every 2nd frame.

Not 100% sure, because we cant see your entire code, but I think you should place that ‘else’ that stops audio on the level of if, that checks for spacebar being pressed. So you chech for input, if its true you playback (so here you need to check if its is already playing, if it is - you do nothing, if its not - you start it), it its false (spacebar not pressed) you stop audio.

2 Likes

Yea, @Medial has it correct. The audio.Stop() part of the conditional is at the wrong if. Apart from it starting and stopping the whole time, it will also not stop (if it was playing) when you let go of the button. That else should be below the if (Input.GetKey(KeyCode.Space))

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms