Only stop the sound when none of the buttons are pressed

So I have been trying to write a script that makes footstep sounds when walking, but I have encountered a problem. If you trigger all the buttons in the method I have done (W A S and D) then if you release one of the buttons the stepping sound stops - but you’re still walking because you kept another button pressed down. (Holding W and D down to walk side ways, release D to walk forward but walking sound stops)

Here is the script

#pragma strict
var Sound : AudioClip;
function Start () {

}

function Update () {
	if(Input.GetKeyDown("w")){
		audio.clip = Sound;
		audio.Play();
	}
	
	if(Input.GetKeyUp("w")){
		audio.Stop();
	}
	
		if(Input.GetKeyDown("a")){
		audio.clip = Sound;
		audio.Play();
	}
	
	if(Input.GetKeyUp("a")){
		audio.Stop();
	}
	
		if(Input.GetKeyDown("s")){
		audio.clip = Sound;
		audio.Play();
	}
	
	if(Input.GetKeyUp("s")){
		audio.Stop();
	}
	
		if(Input.GetKeyDown("d")){
		audio.clip = Sound;
		audio.Play();
	}
	
	if(Input.GetKeyUp("d")){
		audio.Stop();
	}

}
`

`

Instead of starting the sound on key down, consider checking every frame if the key is down, then if so, check if (!audio.isPlaying) to start the audio. You would have most success doing this by not looping the sound, but that might add some unwanted delay between clips.

Another approach would be to have two audio sources, one for side walking and one for forward/backward walking. You could then monitor the keys down and pause one (e.g. pause the forward sound when the D key is pressed, then when D is released, stop the side source and check if W or S is still pressed and resume the forward/backward source.

Maybe yet another approach is to adjust the pitch and volume of the source when side stepping instead of changing the source clip.