Can anyone help with this walking sound script error?

Hi guys,

I’ve wrote a basic script in JavaScript to play sounds while i walk on different surfaces like grass or wood etc…
It’s using collisions with tagged surfaces, all works well but for some reason whenever i press A or D on say the terrain which is meant to play the grass sound effect, it plays wood but when i move using S and D it plays the grass one and i can’t for the life of me figure out what’s wrong.

I was using Horizontal axis and Vertical axis but i thought changing the keys to specific keys might fix it but no luck.

Any help would be greatly appreciated!

Script:

var AudioTimer : float = 0;
var WoodSound : AudioClip;
var GrassSound : AudioClip;

function Update ()
{

if(AudioTimer > 0)
{

AudioTimer -= Time.deltaTime;

}

if(AudioTimer < 0)
{

AudioTimer = 0;

}

}

function OnControllerColliderHit1(col: ControllerColliderHit)
{
if (col.gameObject.CompareTag(“Wood”) Input.GetKey(KeyCode.W) AudioTimer == 0 || Input.GetKey(KeyCode.A) AudioTimer == 0 ||
Input.GetKey(KeyCode.S) AudioTimer == 0 || Input.GetKey(KeyCode.D) AudioTimer == 0 )
{

audio.clip = WoodSound;
audio.PlayOneShot(WoodSound);
AudioTimer = 0.6;

}

//
//
//if (col.gameObject.CompareTag(“Grass”) Input.GetAxis(“Horizontal”) AudioTimer == 0 ||
//Input.GetAxis(“Vertical”) AudioTimer == 0 )
//{
//
//audio.clip = GrassSound;
//audio.PlayOneShot(GrassSound);
//AudioTimer = 0.6;
//
//}

if (col.gameObject.CompareTag(“Grass”) Input.GetKey(KeyCode.W) AudioTimer == 0 || Input.GetKey(KeyCode.A) AudioTimer == 0 ||
Input.GetKey(KeyCode.S) AudioTimer == 0 || Input.GetKey(KeyCode.D) AudioTimer == 0 )
{

audio.clip = GrassSound;
audio.PlayOneShot(GrassSound);
AudioTimer = 0.6;

}

}

thanks! :smile:

function OnControllerColliderHit1(col: ControllerColliderHit)
{
     if(AudioTimer > 0) return;
     if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A)  || Input.GetKey(KeyCode.S)  || Input.GetKey(KeyCode.D))
     {
          if ( col.gameObject.CompareTag("Wood") )
          {
              audio.clip = WoodSound;
              audio.PlayOneShot(WoodSound);
              AudioTimer = 0.6;
          }

          if( col.gameObject.CompareTag("Grass") )
          {
              audio.clip = GrassSound;
              audio.PlayOneShot(GrassSound);
              AudioTimer = 0.6;
          }
     }
}

Thanks WarBladerToo!

but i did manage to fix the code now and all works fine :slight_smile: