Footsteps audio - when walking it sounds like the audio file is rapidly repeating

Hi all,

I’ve recently been assisted with a creating footsteps when I navigate through my game using the directional keys.

The script works perfectly as far as the results show, however, the audio for the footsteps sounds glitchy and repeats my footsteps audio in quick succession. Its the equivalent to imitating an electrical shock noise with your mouth… Has anyone had an issue similar, or can any of you suggest a way around this?

I have pasted my java script code here in the hope someone can assist.

Thanks in advance :slight_smile:
Mac

#pragmastrict

varWalkingOnGrass : AudioClip;

functionUpdate () {
varmoved = false;

audio.clip = WalkingOnGrass;

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();
}
}

Hi Mac. =)
In future, please remember to use the code tags. You can find them in the drop down menu to the left of the floppy disk looking icon when you go to post. This helps people understand your code better.

In answer to your problem, you have the moved variable set to false in the Update function. This means that it will constantly set itself to false many, many times a second, even while you’re character is walking. This causes the audio to constantly stop and start again.

You should declare moved as false somewhere like the Start function. You could use something like this in Update to change movement back to false when you need to.

if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
{
   moved = true;
}
else
{
   moved = false;
}
1 Like

Hey Barachiel

Thanks very much for your help man… It works perfectly for me now! I am also most grateful for your explanation…
(it was driving me crazy)

Duly noted on the code tags too… I knew I was posting incorrectly but didn’t know where to do that exactly :wink:

Thanks again,
Mac

No worries, glad it worked for you. =)