How to add footstep sound?

I’m not good in scripting.Just started to learn that ,I know that this code is bad , because it wont work

function Update () {
if (Input.GetKeyDown("up")){
audio.Play();
}
}

I have basic footstep sound and I want to loop it when character goes z,x axes.

try:

var stepSound : AudioClip;

function Update () { 
if (Input.GetKeyDown("up")){ 
audio.PlayOneShot(stepSound); 
} 
}

I haven’t tested it, but it should work :slight_smile:

This will play your sound once, every time the key up is pressed down.

thank you , but PlayOneShot useless , because I need to loop my sound which is 2 seconds.

You have the possibility to loop the audio file played.

You can use audio.Stop () upon release of the right button.

But isn’t there a cleaner way to do it ? I haven’t look yet at animations (not my priority), so I can’t help…

audio.Play() should work, as long as you’ve defined your clip.

var stepSound : AudioClip;
Audio.clip = stepSound; //-only needs to happen once.

Audio.Play(); //-- should now work.

I believe that in the 2D tutorial they explain how to attach footstep sounds to ground tiles so that it plays at each step the player takes on the said tile. this way you could save a few different sounds and attach them to different ground type prefabs (ie. wood, metal, grass…)