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.
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.
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;
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.
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)
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.
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);
}
}
}
}