Animation Events and Mecanim

Hi there,

I’ve heard that Unity doesn’t have any inbuilt support for animation events for mecanim and instead people for now have to use Curves to do a check.

I’m not very savy though when it comes to looking up curve information so i was hoping someone could post some kind of example about how this might be done.


For my current project I am trying to make a boolean false at the end of a dodge animation. The idea is if a ‘dodging’ boolean is true, the animator will transition from a jog animation to a dodge animation. I have already sorted out my event which males the ‘dodging’ boolean true, and I have setup a curve called EndAnimDodge on my Dodge animation in the inspector’s import settings which turns from 1 to 0 on the very last frame. But I dont know how to access that value (or when) to say “if EndAnimDodge == 0 then ‘dodging’ = false”.

Should it be done in Update? LateUpdate? FixedUpdate?

And how do I access that curve?

I cant use the GetCurrentAnimatorStateInfo(0) stuff because once the animator has transitioned to Dodge, it has no way of getting out of dodge, and i need to reset the flag so that my character can dodge again.
__

Any help / alternative suggestions are much appreciated.

PS. C# ideally

Unity’s tutorial video has segment on using animation curves in scripting to adjust the character controller’s size when jumping: http://video.unity3d.com/video/7362044/unity-40-mecanim-animation-tutorial

As an animation plays, the curve will update a float parameter. You can get the parameter using Animator.GetFloat(): Unity - Scripting API: Animator.GetFloat

Since it’s a float, you should probably check for crossing the zero-axis instead of exactly zero. On the last frame, have the curve drop to -1. As a general-purpose approach, you can trigger events whenever the curve crosses the zero-axis. This way, you can have something like a sine wave to handle multiple footsteps, for example. Also, if the animation isn’t playing, the curve won’t cross the zero-axis, so it won’t touch ‘dodging’.

To check for crossing the axis, multiply the current value times the previous value. If it’s < 0, the sign changed, and you can use SetBool() to set ‘dodging’ to true: Unity - Scripting API: Animator.SetBool

In Update(), something like:

float previous = 0;

void Update() {
    endAnimDodge = animator.GetFloat("EndAnimDodge");
    bool crossedZero = ((endAnimDodge * previous) < 0);
    if (crossedZero) animator.SetBool("dodging", true);
    previous = endAnimDodge;
}

Or buy G1NurX’s Event System for Mecanim and skip all that: Event System for Mecanim [Unity5 supported] | Animation Tools | Unity Asset Store

Ok, so I found out the solution but it took me a while.

First you have to add a parameter into your animator that shares the exact same name as your Curve.

After that you do something along the lines of:

private animator MyAnimator;

if(MyAnimator.GetFloat("parameterFloatName") < 1){
//Do Other stuff
}

The thing you’ll also want to bare in mind is that depending on how your blending works, that parameter value will be affected. So for example you would probably want to do a debug.log every frame to check the parameter value as it might never reach the value you think it will reach.
Also if you are doing the check every update, you will want to make sure your curve has a few frames worth of milliseconds spare, so that the system has a window of opportunity to check the curve value.