How to loop a sound while a button is pressed, but wait for it to finish

Hello people,

I have today just started to try out Unity and have learned a lot so far.
Now I want to give my character a walking sound. I found some example script on the internet and tried that. It works, in a way, when I keep holding the W the sound will play every millisecond causing some sort of static sound. What I would like to have is the sound to play until its finished and if the W is still pressed it should play again et cetera.


This is my current code:

var AudioFile : AudioClip;
function Update() {

if (Input.GetKey (KeyCode.W))
{
    audio.clip = AudioFile;
    audio.Play();
 
}
}

Beware, I’m very nooby and can read/understand script but I can’t write it yet.

Thanks in advance.

that section of the tutorial goes trough the process of adding footsteps aswell.

I recommend going through that tutorial, but this should work.

var AudioFile : AudioClip;
function Update() {

	// If currently pressing W..
	if (Input.GetKey (KeyCode.W))
	{
		// ... and if the footsteps are not playing...
		if(!audio.isPlaying)
		{
			// ... play them.
			audio.clip = AudioFile;
			audio.Play();
		}
	}
		else
		{
			// Otherwise stop the footsteps.
			audio.Stop();			
	}
}