- Home /
How do I stop footstep sound from playing when I release 'w'?
Okay basically I'm trying to write a script that plays an audio clip on loop when I move, but when i take my finger off the w/forwards key, the clip still keeps playing. I'm totally new to scripting and unity so i'm not too sure how to sort this out, i was thinking i'd have to make another piece of script where audio.loop = false but not sure how to do it. any ideas?
var AudioFile : AudioClip;
function Update() {
if (Input.GetKeyDown (KeyCode.W))
{
audio.clip = AudioFile;
audio.loop = true;
audio.Play();
}
}
Answer by Fornoreason1000 · Apr 29, 2013 at 08:39 PM
ok, See how you made it loop? you don't want that on when w is released, just use an else block and set audio.loop back to false. you could also make if(!Input.GetKeyDown(keycode.W)) which is the opposite of if (Input.GetKeyDown (KeyCode.W))
else {
audio.loop = false;
}
//or
else if((!Input.GetKeyDown(keycode.W)) {
audio.loop = false;
}
be sure to check the tutorials Unity offers as well as its documentation
thanks so much buddy! i'll try that now to see if it's sorted. Honestly thanks a lot.
so should my final script be
var AudioFile : AudioClip;
function Update() {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.W))
{
audio.clip = AudioFile;
audio.loop = true;
audio.Play();
}
else if((!Input.Get$$anonymous$$eyDown(keycode.W)) {
audio.loop = false;
}
}
yeah, you could just use else in this case, (since there's only two possible outcomes). I was just showing you some extra ropes