Footstep frequency

Hi All,

I wonder if any of you could help. I have been using this script for my first person controller footstep sound, and it works great. However, I would ideally like it so that when I press the vertical up key on my keyboard ONCE, it would equate to ONE footstep. Right now, I have to press the vertical “up” key for a couple seconds for the footsteps to begin in a continuous motion.

Here is my script, for clarity:

 function Update() 
{
 if(Input.GetButtonDown("Vertical"))
    {
      audio.Play ();
    } 
 if (Input.GetButtonUp("Vertical"))
    {
       audio.Pause();
    } 
 
}

Does anyone have any idea on how I could do this?

Thanks!

Animation events are really handy for this sort of thing. Each time your character’s animation puts their foot on the ground, the animation can call a function (say, “OnFootDown”), which can play a sound, emit some particles, or do whatever else you need.

Interesting! I am a very new at all this though; would you mind clarifying how I would be able to do that?

Thanks so much.

Animation events only work with the legacy animation system, so if you are using Mecanim, it won’t work. The only audio pause function I can find is the one connected to audiolistener. Perhaps have a good look at the audio functions and find what you are looking for, unless you are using the legacy animation system.

Hmm, I am trying to figure it out. All I want is for 1 press of the vertical button on my keyboard = 1 footstep SOUND.

I don’t really care about the visual effects since this is a first person controller.

Is there no other way to do this?

Set it to play once. It’s going to be small, so you could probably just name a clip and set that one to play once and another to loop. Otherwise you could do it through code, also, or you could use stop I think, which will cause it to start over when you use play the next time, or use a coroutine and stop it after so much time. I have my loop timed to the walk cycle, so there’s no way I would mess with it. I would use audacity to make another one that started with no pause and have it play once.

fire7side, thank you so much. I tried your first suggestion of adding another audio file and scripting it to play once, and it worked!

Thanks again! =)