Audio Play/Stop question

Hi, I am trying to get a footstep sound put in my game but it’s just not quite working. The script below is what i currently have, and it does work, but if you lift up lets say… the W key while your still holding the S key, the audio will stop. How would i make that not happen?

function Update () {

	if (Input.GetKeyDown("w") || Input.GetKeyDown("a") || Input.GetKeyDown("d") || Input.GetKeyDown("s")) {
		audio.Play();
		}	
	
	if (Input.GetKeyUp("w") || Input.GetKeyUp("a") || Input.GetKeyUp("d") || Input.GetKeyUp("s")) {
		audio.Stop();
		}
		
		
	if (Input.GetKeyDown("left shift")) {
		audio.pitch = 1.8;
		}
		
	if (Input.GetKeyUp("left shift")) {
		audio.pitch = 1.55;
		}
		

		
}

It’s just goiing to be a little tedious. You need to remember which key has been pressed, and only stop the sound when ALL depressed keys are released like this:

Code:

var wDown = falsle;
var aDown = false;
var dDown = false;
var sDown = false;

function Update () {

if (Input.GetKeyDown("w")) 
{
   wDown = true;
   audio.Play();
}

if(Input.GetKeyDown("a")) 
{
   aDown = true;
   audio.Play();
}

// etc. for the others.

	
if (Input.GetKeyUp("w") )
{
   wDown = false;
}

if (Input.GetKeyUp("a") )
{
   aDown = false;
}

// etc for the others

// to stop the audio:
if(!wDown  !aDown  !sDown  !dDown)
{
		audio.Stop();
}
}

It’s tedious, like much of programming, but it will work and be fast enough for now.

Thank you very much! that made it work :slight_smile: