Play footstep sound loop while holding any of the WASD keys.

I want to play and loop a sound if I am holding down one of the WASD keys. How would I go about doing this?

Hi, i know this is an old thread, but i still wanted to share.
if you want to make the sound fade in and out depending on if the player is mooving or not, i actually made the audioclips volume dependant on the magnitude of the characters movement.

    audio.clip = playerWalkSound;
	audio.volume = horizontalMovement.magnitude * 0.01;
	
	if(horizontalMovement.magnitude > maxWalkSpeed * playerWalkSoundThreshholdRatio){
		if(!playerWalkSoundCreated){
			audio.Play();
			playerWalkSoundCreated = true;
		}
	}else{
	
		audio.Stop();
		playerWalkSoundCreated = false;
	}

i don’t know if this is stupid or not, but i wanted to throw it out here :slight_smile:

I've never tried putting in audio before, but perhaps you're looking for something like this?

var AudioFile : AudioClip;

function Update()
{

    if (Input.GetKeyDown ("w"))
    {
        audio.clip = AudioFile;
        audio.Play();
    }

    else
    {
        audio.clip = AudioFile;
        audio.Pause();
    }

}

It should work. =)

Add an AudioSource component to the object, and set it to looping in the editor. Then create a script and add it to the object too, after adding this code in the Update method:

if ( Input.GetButtonDown( "Horizontal" ) || Input.GetButtonDown( "Vertical" ) )
        audio.Play();
    else if ( !Input.GetButton( "Horizontal" ) && !Input.GetButton( "Vertical" ) && audio.isPlaying )
        audio.Stop(); // or Pause()

I haven't actually checked it, but it should work. You should get the idea.

be sure to check the (is grounded) topics because you dont want to press the jump button and the wasd keys and in the air, hear footsteps
Here is: Unity - Scripting API: CharacterController.isGrounded

var AudioFile : AudioClip;

function Update()
{

if (Input.GetKeyDown ("w"))
{
    audio.clip = AudioFile;
    audio.Play();
}

else

  if (Input.GetKeyUp ("w"))
{
    audio.clip = AudioFile;
    audio.Pause();
}

}

This is currently working fine in my game… probably heaps wrong in the ways of further development, but as it stands it WORKS :smiley: need an audio source on character with this script.

along with your footstep sound clip.

var AudioFile : AudioClip;

function Update() {

if ( Input.GetButtonDown( "Horizontal" ) || Input.GetButtonDown( "Vertical" ) )
{
     GetComponent.<AudioSource>().clip = AudioFile;
     GetComponent.<AudioSource>().Play();
}   
 
else if ( !Input.GetButton( "Horizontal" ) && !Input.GetButton( "Vertical" ) && GetComponent.<AudioSource>().isPlaying )
{
     GetComponent.<AudioSource>().clip = AudioFile;
     GetComponent.<AudioSource>().Stop();
}

}