- Home /
button hold fires audio every update
Hi Im trying to do a walking sound effect everytime I press a key down which plays and then when I let go the sound stop which I have achieved at the moment by using keyUp and keyDown in void OnGUI.
The problem is if I hold the key down the sound fires over and over again more than once every update i presume.
How would I go about holding the key down and it not firing multiple times just the once until I let go of the button then reset so that whenever I press the key down the sound begins playing from the start again.
I read on unity answers that putting my code in void Update would solve this but when I did it didn't work and I got NullReferenceException:
Unity basically didn't like my if statement. Also tried the following link to fire off the sound which didn't work http://unity3d.com/support/documentation/ScriptReference/Event-button.html
So figure I'll stick with the code I have written but need a way to make the sound just play once when button is held done.
void OnGUI ()
{
if(Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.W)
{
audio.PlayOneShot(footsteps_audio);
{
{
Answer by Bunny83 · Nov 08, 2011 at 10:39 AM
That's a design problem. In OnGUI the key events you receive are the wrapped keyboard events. If you hold down a key it is repeated (after the configured delay) with the keyboard repeat-rate (usually set in the OS). Those events are deigned for GUI stuff like a TextField to input a string. For pure event handling you should use the Input class. You need Input.GetKeyDown.
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
[...]
Thanks Bunny that worked a treat. So going to go with your method it's a shame about the design flaw but as long as this works which it does I'll get over it just fine ;). Thanks again.
No problem ;) but by "design problem" i don't ment there's something wrong on Unitys side. OnGUI is ment for GUI usage and in most cases you want / need the typical keyboard-repeat rate, otherwise you would go crazy when you want to move your cursor in a textfield 10 characters to the left.
OnGUI is for all kind of GUI related stuff.
Update is for your gamelogic
So it's a feature, not a bug ;)
Answer by cj_coimbra · Nov 08, 2011 at 10:33 AM
I think you can read the audio state with "audio.isPlaying" then just check in your input if the audio already started.
void OnGUI ()
{
if(Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.W)
{
if (audio.isPlaying)
return;
else
audio.PlayOneShot(footsteps_audio);
}
}
Thanks cj unfortunately I get an error saying that isPlaying cannot be used like a method but i see how it would have worked though.
I tried bunny's method and that works a treat so im going to stick with that.
Thanks again.
Alright, my mistake. It´s "audio.isPlaying" without the ()... if you ever need to use it.
Thanks cj will keep this and use this aswell for my parallel project can never have too many solutiions to a problem plus haven't used the isPlaying function before so would be good to try out. Thanks.