- Home /
Animation layers, duplicate AnimationEvents. (SOLVED)
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 ?
Answer by LaurynasLubys · Mar 28, 2016 at 10:30 AM
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.
Thanks @lauris47 very practical no code solution.
Answer by LaurynasLubys · Mar 27, 2016 at 08:34 PM
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
}
}
}
Answer by Kutiman · Mar 28, 2016 at 12:57 AM
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
}
That is what I'm doing already. Problem is - no matter the conditions, both sounds are playing. When I run the character, and I am not exhausted and in layer 0 of animations, everything is fine.
I have a function, to detect if character is exhausted, in it I change state of exhausted to true. When it is true, I switch layer weights of 0 to 0 (not exhausted layer of animation) and 1 to 1 (exhausted layer). I have public bool isExhausted, and I print out layer wights in inspector. So I know the conditions switch 100%. However when I am in layer 1, and its weights are 1 and layer 0 has wights 0 I can still hear footsteps of layer 0, even when code does not allow to enter if statement, it does somehow. I think it is a bug in Unity, people just somehow go around it.
I could solve this stupidly by calling event by different string name, (footStepsNormal and footStepsExhausted) and then I would duplicate the code for playing foot steps sound. Just like it was solved here: http://answers.unity3d.com/questions/755073/how-to-ignore-anim-events-on-layers-with-weight-se.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612073444im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I call an animation event on a deactivated gameobject in unity3d c# 0 Answers
AnimationEvents - Get All Events? 0 Answers
Can one Animation Event be used for a timespan (multiple frames)? 1 Answer
How to create animation event at specific keyframe at runtime 2 Answers
Mecanim events firing incorrectly when clip playing backwards? 1 Answer