Triggering audio for walking when using directional keys (up, down, left, right)

Hi everyone,

I’m new to this forum and also quite new to unity + programming script.
I’m fumbling my way though making a first person player game and need help or direction with my current code. I want to be able to play the audio when I move the right, down, left keys. Currently it works when I move up on the directional keys.

Could anyone help me understand what I need to include within my script to get that working also?

I would be very grateful and hope someone can help me move forward with this (pardon the pun)

Mac

Here is my script -

#pragmastrict

vartheDoor : GameObject;
varDoorAlreadyCreaked = 0;
varWalkingOnGravel : AudioClip;

functionStart () {

}

functionUpdate () {

audio.clip = WalkingOnGravel;

if(Input.GetKey(KeyCode.UpArrow))

{

if (!audio.isPlaying)
audio.Play();
}
else
{

audio.Pause();

}

}

I guess you want something like this:

#pragma strict

var theDoor : GameObject;
var DoorAlreadyCreaked = 0;
var WalkingOnGravel : AudioClip;

function Update () {
    var moved = false;

    audio.clip = WalkingOnGravel;

    if(Input.GetKey(KeyCode.UpArrow))
    {
        moved = true;
    }

    if(Input.GetKey(KeyCode.DownArrow))
    {
        moved = true;
    }

    if(Input.GetKey(KeyCode.LeftArrow))
    {
        moved = true;
    }

    if(Input.GetKey(KeyCode.RightArrow))
    {
        moved = true;
    }

     if(moved) {
        audio.Play();
     }
    else
    {
        audio.Pause();
    }
}
1 Like

Thanks so much for responding so quickly PGJ! When I try it, an error occurs stating -
Assets/Walking audio.js(1,8): BCE0044: unexpected char: ‘s’.

That relates to the first line and the s in #pragmastrict

Why would that happen?

Thanks in advance and I really appreciate your input even this far!

Ooops, when I copied your code, I didn’t see that there was a space missing and of course it shouldn’t be a bool, but a var since your code was UnityScript. I’ve edited the code above and it should work now.

You’ve helped me out massively PGJ… It works when I use any of the directional keys, but for some reason the audio clip when auditioned sounds like it a glitchy / quick repeated version of the audio clip that I have (Im not going to trouble you any further with that side issue)

Thank you very much for your help and for being so generous with your time!!

I will try figure it out from here :slight_smile:

Kudos
Mac