Get length of animator state/transition?

There seems to have been a few queries around this area, such as:

http://forum.unity3d.com/threads/162505-Get-animation-length-in-Mecanim

However there seems to be no answers as of yet. The problem I have is that I want to change from the state “walking” to “punch” however I need to check for button presses to see if I need to then go to “punch-2” or back to “idle”.

So simplest way to achieve this seems to be change the animation, then find out how long is left in the animation, and give that as the period to listen for button presses, however it seems like the animation info Length property is not accurate, so is there some preferred method or best practice around doing this?

Seems like AnimationEvents added thru animation viewer are not compatible with mecanim.

But you can add new curves and events in the model’s Animations tab. Curves and events added thru here are usable in mecanim.

It’s well explained here (especially the mecanim video tutorial): http://answers.unity3d.com/questions/378420/trigger-event-on-specific-frame-of-an-animation.html

So i have added new curve called used starting at 0 ending at 1. Added new float called used in my animator controller. In my code i then did animator.GetFloat("used ") and it works !

Quick image tutorial

If it helps anyone else I ended up re-visiting this problem and the solution I found was to use both the current animation state and the current clip state (or next) to get the details on if the current state was one we cared about, and if so get the normalized time from it, then get the clip state info and get the clip from it, then multiply the clip length by normalized time to give an indication as to when the clip will be over.

            var animationState = _animator.GetCurrentAnimatorStateInfo(0);
            if (animationState.nameHash != YourAnimationNameHash) { return; }

            var animationClips = _animator.GetCurrentAnimationClipState(0);
            if (animationClips.Length == 0)
            { throw new Exception("No clips associated with animation"); }

            var animationClip = animationClips[0].clip;
            var animationTime = animationClip.length*animationState.normalizedTime;