- Home /
This question was
closed Sep 16, 2013 at 12:53 PM by
Fattie for the following reason:
The question is answered, right answer was accepted
Question by
kaini_industries · Sep 16, 2013 at 12:12 PM ·
audioloopfootsteps
Random footstep sounds not looping
Hi there,
I'm having an issue where a script I have is supposed to loop randomly between four different footsteps sounds when forward is pressed. The problem is that when 'W' is pressed, it plays one of the four clips at random, but won't loop until I release 'W' and press it again.
Any help is appreciated!
#pragma strict
var Steps : AudioClip[];
function Update() {
if (Input.GetKeyDown(KeyCode.W))
{
audio.clip = Steps[Random.Range(0,Steps.length)];
audio.loop=true;
if (!audio.isPlaying)
{
audio.Play();
}
}
else (!Input.GetKeyDown(KeyCode.W))
{
audio.loop=false;
}
}
Comment
try Get$$anonymous$$ey , Get $$anonymous$$ey Down will only return true once, as soon as you press it.
Best Answer
Answer by kaini_industries · Sep 16, 2013 at 12:47 PM
I solved the issue:
function Update() {
if (Input.GetKeyDown(KeyCode.W))
{
audio.clip = Steps[Random.Range(0,Steps.length)];
audio.loop=true;
if (!audio.isPlaying)
{
audio.Play();
}
}
if (Input.GetKeyUp(KeyCode.W))
{
audio.Stop();
}
}