- Home /
How to stop footstep sound when jumping, but continue when hitting the ground?
I'm new to Unity (and scripting in general), and I have started making a first person adventure game.
I have been able to get sound effects for walking and jumping to work, but the problem is that the walking sound plays even in the air, when jumping and pressing any of the movement keys in the air.
I've tried stopping the footstep sound with audio.Stop when the jump button is pressed, but this leaves another problem: The footstep sound doesn't continue when the player hits the ground, if one of the movements keys was pressed the whole time (a lot of cases when jumping forward and continuing movement after landing).
Is there a(n easy) way to make the footstep sound continue after landing?
Here's my sound script so far (without the audio.Stop, since it didn't work like a wanted):
var jumpSound : AudioClip;
var stepSound : AudioClip;
function Update () {
if ( Input.GetButtonDown ( "Jump" )){
audio.PlayOneShot(jumpSound); }
if ( Input.GetButtonDown ( "Horizontal" ) || Input.GetButtonDown ( "Vertical" )){
audio.clip = stepSound;
audio.loop = true;
audio.Play();
}
if ( Input.GetButtonUp ( "Horizontal" ) || Input.GetButtonUp ( "Vertical" )){
audio.Stop();
}
}
Answer by Pat · Aug 19, 2011 at 11:48 PM
Instead of doing this with code, you should use animation events and sync the sounds there. That way when you jump and you play the jump animation the animation events for walking won't get fired. You can read more about animation events here:
http://unity3d.com/support/documentation/Components/animeditor-AnimationEvents.html,
Your answer
Follow this Question
Related Questions
Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 Answers
Audio not coming out of speakers 1 Answer
Stop audiosource clip from playing 1 Answer
Can't play multiple sounds at once 2 Answers
Why is Unity3d strictly using PCM Buffer size with 32bit floating in AudioClip? 1 Answer