Play sound only while a key is pressed?

Heya. I’m honestly not sure at all how to do this. I’m a total noob.

var Sound : AudioClip;

  function Update () {

 if (Input.GetKeyDown("w"))
 {
   
	 audio.Play(Sound);
 }
 if (!Input.GetKeyDown("w"))
 {
   
	 audio.Stop();
 }
 
}

Any idea how I can get this to work?

If you’re wondering, this is for a footstep sound.

It should be this instead :

var Sound : AudioClip;

function Update () {

if (Input.GetKeyDown(“w”))
{

audio.PlayOneShot(Sound);
}
if (Input.GetKeyUp(“w”))
{

audio.Stop();
}

}

Yessss! Thank you kindly sir!

-Cheers.

1 Like

yessssssssssssssssssssssssssssssssssss