How To Find Animation Clip Length

I’m using multiple Animator Controllers (one for each enemy model) with four states: Idle, Attacking, Damage, Dead

I need to find the length of Attacking, Damage, and Dead animations on the current enemy so I can use a timed delay for other actions.

I understand how to get Animation time but in this case I have the Animator/Controller on the models, not an Animation component.

Thanks in advance.

2 Likes

anim.GetCurrentAnimatorStateInfo(layer).length gives you the length of the current animation that’s playing. There is no way to get any information whatsoever about states that are not currently playing without generating that information and adding it to the gameobject before you start playing.

8 Likes

Thanks Baste.

I was actually able to find a solution. Here’s the code I created to assign the value to a float:

    public float attackTime;
    public float damageTime;
    public float deathTime;
    public float idleTime;

    private Animator anim;
    private AnimationClip clip;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
        if(anim == null)
        {
            Debug.Log("Error: Did not find anim!");
        } else
        {
            //Debug.Log("Got anim");
        }

   UpdateAnimClipTimes();
    }
    public void UpdateAnimClipTimes()
    {
        AnimationClip[] clips = anim.runtimeAnimatorController.animationClips;
        foreach(AnimationClip clip in clips)
        {
            switch(clip.name)
            {
                case "Attacking":
                    attackTime = clip.length;
                    break;
                case "Damage":
                    damageTime = clip.length;
                    break;
                case "Dead":
                    deathTime = clip.length;
                    break;
                case "Idle":
                    idleTime = clip.length;
                    break;
            }
        }
    }
58 Likes

Would you look at that, I was completely wrong!

17 Likes

Took me a little bit. I just learned to use any sort of mecanim or 3D modeling last week so it’s all new to me. Regardless, I really appreciate you trying to help. It’s frustrating to post a question here and not get anything.

1 Like

if you’re going for a timed delay, doesn’t this method only work if each animation is set to a speed of 1?

edit: i’m guessing you can reference the animation speed as well as the length and just multiply the result together to get your time delay?

You can also manually do it by dividing the number of frames by the framerate, like 180/60 = 3 seconds

3 Likes

THANKS MAN! Smart solution. It helped me a lot

Exporting from Maya, all my animations are named ‘Take 001’

Otherwise this could have been a good solution to my problem

Is there a possiblty to do this to when you are using AnimatorOverrideController? Becaues with these the Name changes and the above code dosent work.

All I found was this code:

        AnimationClip[] clips = anim.runtimeAnimatorController.animationClips;
        //17 is the current number of the attack animation, this can be seen in an AOC
        attackAnimationClipLenght = clips[attackAnimationClipNumberInAOC].length;

Is there a better option I just cant find?

Just a heads up with this - I was also really struggling with the AnimatorOverrideController scenario:

My use-case was that I was setting a trigger for an animation, and then wanting to know when the animation was complete. But of course, just because I’d set the trigger did not mean that it was the current (or even next) Animator state info - these will only get set at some point in the future.

However, I noticed that you can actually tag your animations in the base animator controller - and these tags persist onto the inherited animations. So my code was:

private bool AnimationComplete(string theTagIPutOnTheBaseAnimation)
    {
        var state = Animator.GetCurrentAnimatorStateInfo(0);

        return state.IsTag(theTagIPutOnTheBaseAnimation) && state.normalizedTime >= 1;
    }

EDIT: by tag - I mean, open the base animator controller in the editor view, then click on a state, and you’ll see a “tag” field for it in the inspector.

7 Likes

you can rename your animation clips, just click on them → go to the inspector → top right corner → edit (dont forget to tap Enter once you renamed the clip and click save after that)

1 Like

I strongly suggest you try using Maya’s Game Exporter for files with animation. It makes managing clip names & multiple clips in one file a lot easier.

1 Like

I used it at an animation event and it did give different values each time. I’m not sure if it’s working as you explained. On the other hand, johnnieZombie’s solution is more robust and working well.

Found solution for the case when we are using multiple animator controllers (1 - base animator and many which override base animator):

        public Animator animator;
        private AnimationClips<HeroAnimationType> _animationClips;

        private void Awake()
        {
            _animationClips = new AnimationClips<HeroAnimationType>(animator.runtimeAnimatorController);

            AnimationClip idleAnimationClip = _animationClips.GetAnimationClip(HeroAnimationType.Idle);
        }

Your enums must be named exactly like animations used in the base animator controller. Animations assigned to overridden animator controllers could have any name.
Strongly recommends Rename animations used in base animator instead of creating enums with the current animation’s name. For example, if you have an animation with the name “idle-001” rename it to “Idle”.

    public enum HeroAnimationType
    {
        Idle,
        Attacking,
        Damage,
        Dead
    }

And general class which can get animation clip by enum:

    public class AnimationClips<TClipType> where TClipType : struct, Enum
    {
        private readonly RuntimeAnimatorController _runtimeAnimController;

        private Dictionary<TClipType, AnimationClip> _animationClips;

        public AnimationClips(RuntimeAnimatorController runtimeAnimController)
        {
            _runtimeAnimController = runtimeAnimController;

            InitializeAnimationClips();
        }

        public AnimationClip GetAnimationClip(TClipType clipType)
        {
            if (ContainClip(clipType))
            {
                return _animationClips[clipType];
            }

            return null;
        }

        private void InitializeAnimationClips()
        {
            _animationClips = new Dictionary<TClipType, AnimationClip>();
         
            if (_runtimeAnimController is AnimatorOverrideController)
            {
                InitializeFromOverridenController();
            }
            else
            {
                InitializeFromBaseController();
            }
        }

        private void InitializeFromBaseController()
        {
            AnimationClip[] clips = _runtimeAnimController.animationClips;

            foreach (AnimationClip clip in clips)
            {
                if (Enum.TryParse(clip.name, out TClipType type))
                {
                    if (_animationClips.ContainsKey(type))
                    {
                        continue;
                    }
                 
                    _animationClips.Add(type, clip);
                }
            }
        }

        private void InitializeFromOverridenController()
        {
            AnimatorOverrideController overrideController = _runtimeAnimController as AnimatorOverrideController;
            var overrides = new List<KeyValuePair<AnimationClip, AnimationClip>>();
         
            overrideController.GetOverrides(overrides);

            foreach (var overrideClip in overrides)
            {
                if (Enum.TryParse(overrideClip.Key.name, out TClipType type))
                {
                    if (_animationClips.ContainsKey(type))
                    {
                        continue;
                    }
                 
                    _animationClips.Add(type, overrideClip.Value);
                }
            }
        }
    }

Going to lock this now as it has multiple solutions!

Good thread folks.

1 Like