Play Looped Sound?

I am making a simple First Person game and im trying to make it when im holding W, a footstep sound repeats.

Im only a beginner in Coding so i need some help, This is what i got but im not sure how to make it loop

Thanks,

var StepSound : AudioClip;

function Update() {

if (Input.GetKeyDown("w")) { audio.PlayOneShot(StepSound); } } @script RequireComponent(AudioSource)

else you have to select your sound in folder view,when u select see the inspector box,and chek the "loop". then use this script:

var StepSound : AudioClip;

function Update() {

if (Input.GetKey (KeyCode.W)) { 

audio.clip = StepSound;
audio.Play();

           } 

} 

if u don't found the chekbox for loop,just use this script:

var StepSound : AudioClip;

function Update() {

if (Input.GetKey (KeyCode.W)) { 

audio.loop = true;
audio.clip = StepSound;
audio.Play();

           } 

} 

GetKeyDown() returns true during the update that the specified key was pressed; as such, that code will only play the footstep sound when the player begins moving forward (the sound won't loop).

GetKey() returns true during any update in which the specified key is held down. However, if you replace GetKeyDown() with GetKey(), you probably still won't get the expected results, since you'll be restarting the sound every update.

Options include starting the sound with looped playback when the key is pressed and stopping it when the key is released (although you'll probably need to add some extra logic to accommodate multiple keys), using a timer of some sort, and/or triggering the footstep sounds based on motion rather than input events. (There are lots of threads in the forums on creating 'footstep' sound effects, so if you haven't already, you might search there.)

Instead of PlayOneShot, try assigning StepSound (your audio clip) to be the Audio Clip of your GameObject's AudioSource.) (Easily done in the inspector, unless your object plays more than one sound.)

audio.Play();

Also, look at the audio source in the inspector and make sure you've checked the Loop checkbox. (Or, set audio.loop = true just before you call Play).

So that will get the sound to loop. The remaining problem is starting and stopping it at the right time. You could can start on GetKeyDown like you are doing, on GetKeyUp call:

audio.Stop();

One more thing: I haven't tried this, but if the stopping sounds too abrupt, you might be able to set

audio.loop = false;

on key up instead, so that it will stop the sound after it's finished playing, rather than cutting it off. If you do that you would set loop = true just before you start playing it again.

Have a look at AudioSource documentation for other useful methods.

Here is a nice simple way of doing it.

var StepSound : AudioClip; function
Update() { if (Input.GetKeyDown
(KeyCode.W)) { audio.loop = true;
audio.clip = StepSound; audio.Play();
}
else if (Input.GetKeyUp (KeyCode.W)) {
audio.Stop();
}
}

Just make sure your loop box is checked for the audio source, and their is appropriate silence at the end of the loop depending on the speed of running :slight_smile: