- Home /
Mecanim animation layers and animation events
I have a Base Layer and multiple other override layers with different animations.
Normally only the base layer is active and all the other layers have their weights set to 0.
Now I started putting animation events on the animations and I noticed that the events get triggered on every layer even if their weight is set to 0 and I get spammed by sound from every layer.
Is this the correct behavior of the layer system? And how do I prevent an inactive layer (weight==0) from triggering animation events?
Did you find any solution ? I got the same problem, my temporary solution is to active a flag in the first function call and add a if condition at the start of this function, but it's quite unstable...
Answer by grasmann · Oct 20, 2015 at 12:01 PM
I have the same problem. What I came up with now is this extension:
using UnityEngine;
public static class Extensions {
public static int GetDominantLayer(this Animator Animator) {
int dominant_index = 0;
float dominant_weight = 0f;
float weight = 0f;
for (int index = 0; index < Animator.layerCount; index++) {
weight = Animator.GetLayerWeight(index);
if (weight > dominant_weight) {
dominant_weight = weight;
dominant_index = index;
}
}
return dominant_index;
}
}
Basically it returns the index of the layer with the highest weight. In my animation events I'm passing the layer index I want this event to happen on via the int parameter. In the function of the animation event I just have to check if the int parameter matches the dominant layer index.
void MyEvent(AnimationEvent evt) {
if (evt.animatorClipInfo.weight > 0.5 &&
evt.intParameter == animator.GetDominantLayer()) {
// Do stuff
}
}
Answer by meat5000 · Apr 27, 2014 at 10:48 AM
The animation 'ticker' will keep tracking through in order to help with animation syncing. You must make sure the exit parameters are properly set so the animation is not playing, just tracking.
It is simple enough to set the default state for each layer to be an Empty State and this will work with no errors or problems.
Answer by LaurynasLubys · Mar 26, 2016 at 04:04 PM
I have the same problem. Any other solutions to this ?