Hey, this was asked before, but I cannot see if this was answered ever since 2010 in this forum.
I have two layers of animation, 0 for normal animations and 1 for “player exhausted” animations.
I’m calling events on 0 layer to play foot step sounds and it works great. However, when I switch to layer 1 via animator.SetLayerWeight(1, 1); events from layer 0 is still being called all the time, so I get duplicate sounds.
- I tried setting layer 0 to 0, same result;
- Layer 1 is overriding 0 and opposite.
- Tried checking what layer has higher weight value, and only then playing sounds. (check is ignored, since both layers are playing?)
Any solution to this, workaround ? Maybe using events is a bad idea, and I should call function to play sounds on collision enter ?
I solved it.
I noticed that both layers are always playing no matter the conditions (layer weights, bools etc)
So I created an empty state in layer 0 and marked it as default. (which does not have any animation - therefor no sounds events are called)
Then when character is exhausted, “tired”, it switches to that empty animation state and stays on it, unless character is no longer exhausted. In this case, when character is exhausted and in layer 1, “tired”, layer 0 is in that empty state without animation events ever called.
public void PlaySounds(string name)
{
if (m_Animator.GetLayerWeight(1) == 1 && isExhausted)
{
//will call AnimationEvents of layer 0 in adition to layer 1
if (name == “steps”)
{
//playsound
}
}
if (m_Animator.GetLayerWeight(0) == 1 && !isExhausted)
{
//will call only layer 0 events
if (name == “steps”)
{
//playsound
}
}
}
Read here: http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html
using this, you simply go to your walking animation, and in the inspector, go down to events and click the arrow to open it, slide with the red marker exactly to the frame of when the foot touches the floor and add an event there. Do this for every place a foot touches the floor in your animation.
The small dialog box will open and simply write the name of the function that is in the script. example PlayStepSound
, but only the name with no ()
.
That’s it. You can do it with only one function, and the rest from the animation event dialog.
public function PlayStepSound () {
//Play sound
}