Search Unity

Blend trees + Animation Events

Discussion in 'Animation' started by electro_unity, Mar 9, 2019.

  1. electro_unity

    electro_unity

    Joined:
    Nov 29, 2015
    Posts:
    64
    When you use animations with events inside a blend tree, all events will trigger no matter which blend tree's animation is running at the moment. Is there a way to avoid this?
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Nope. The same is true when you have multiple animations active during a transition.

    The only workaround I know of is to give each event a parameter to identify the clip its coming from and use that to filter out the ones you don't want. It's not at all a good solution, but I don't know of anything better.
     
  3. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    I was just fiddling around with some events and it turns out I was wrong.

    If you give your receiver method an AnimationEvent parameter, Unity will give it all the details of the event which can be used to access the clip and its weight. So your method will always get triggered, but you can just ignore any events with 0 weight if you want to.

    Edit: note that this will allocate some garbage every time the event is invoked. The same applies to string parameters, but not the other types.
     
    Last edited: Feb 23, 2024
  4. DungeonBrickStudios

    DungeonBrickStudios

    Joined:
    Jun 9, 2015
    Posts:
    69
    Sorry to revive such an old thread, but I'm having this same issue. The solution above is interesting, but the thing about it is that often the weight will not be 0. There might even be a point where both animation from and to have weights of 0.5 while blending. So the undesired event from the previous animation will still go through. I'm trying to find an elegant solution to this, but have not had any luck so far. Telling it to ignore any weights less than 1 will filter out all the old animation's stuff, but it will also filter out any events that occur in the new animation while that's blending in. I wish there was a way to determine if the clip is a "To" clip or a "From" clip. Like just getting its position in the transition itself. I've not been able to find anything for that yet though.

    Edit: One hacky solution I found was using something like this:
    Code (CSharp):
    1. if (animationEvent.animatorStateInfo.normalizedTime > 0.5f && animationEvent.animatorClipInfo.weight < 1)
    2.                 return;
    So kind of like saying "if we're in the latter half of an animation and yet somehow its weight is less than 1, we assume we're transitioning out of it". How good and durable that assumption is, well.... that's what makes it a less than ideal solution. It's possible it will break if things are going fast enough.
     
    Last edited: May 14, 2023
    Santi0512 likes this.
  5. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Yeah, I never found a better solution in the standard Animation Events system and ended up implementing my own event system in Animancer which only triggers events from the current animation by default. Surely you have access to something less hacky than time though, since that would only work reliably for exit time transitions and not any sort of interruptions.
     
    DungeonBrickStudios likes this.