AnimatorStateInfo.normalizedTime breaks with different animation speeds

Hey all,

I’m having some trouble trying to implement some simple animation events into my project and it has to do with the normalizedTime that’s reported by the Animator.

I have a character with an idle and attack animation that have transitions from one to the other based on an IsDead bool in the AnimatorController. That’s all working fine but when I try to get the specific time in the attack animation I’m getting incredibly weird and useless values.

I’m trying to implement events to turn the character’s attack collider on and off based on his position in the animation since there’s some wind up time I don’t want it active. I’m using Mixamo’s free animator event script which just sends a message when the normalizedTime of the animation goes above the value you specified (0.15 and 0.9 in my case).

I noticed that the collider was being toggled three times during his swing instead of two and attached the following simple script to the character to see what was going on:

public class testNormalizedTimeBug : MonoBehaviour {
	Animator animator;
	
	void Start() {
		animator = gameObject.GetComponentInChildren<Animator>();
	}
	
	void Update() {
		AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0);
		
		if(info.IsTag("attack")) {
			Debug.Log(info.normalizedTime);
		}
	}
}

I found that the Debug.Log()'s were reporting times that started at 0.1 and ended around 1.3 for every swing, hitting the .15 event marker twice. The attack animation is a single non-repeating clip so I was confused how it was going over 1.0 for the time.

Eventually I found out that the normalizedTime seems to vary wildly if you change the speed of the animation in the AnimatorController. I was playing the animation at 1.5 speed because it was a bit slow to feel good in game.

I then pumped the attack animation speed up to 15 to see what would happen and the debug logs reported that the animation started at 4.7 and ended at 7.5. Again, for a non-looping animation this makes no bloody sense.

At 0.2 speed the logs start at 0.04 and end at 0.82. Every time I’m playing the exact same animation only changing it’s playback speed.

Even running the animation at normal speed the logs never printed anything less than 0.1 for the normalizedTime and seemed to ramp the number up slowly over time instead of a smooth increase. E.g:

at the start

0.119248
UnityEngine.Debug:Log(Object)
0.123975
UnityEngine.Debug:Log(Object)
0.125076
UnityEngine.Debug:Log(Object)

and near the end

0.86283
UnityEngine.Debug:Log(Object)
0.95204
UnityEngine.Debug:Log(Object)
1.000311
UnityEngine.Debug:Log(Object)

Am I crazy or do the values reported by AnimatorStateInfo.normalizedTime make no sense at all? Mecanim has been pretty smooth to work with so far but this is driving me up the wall along with the fact that there’s no way to change a specific animation’s speed through code without using a blend tree.

Well that was long winded but hopefully someone might know what’s going on and I’m just missing something obvious.

TLDR: AnimatorStateInfo.normalizedTime is reporting wildly inconsistent values when playing the exact same non-looping animation at different speeds.

Ok, so after ages of fiddling I finally figured out part of the problem. It turns out that GetCurrentAnimatorStateInfo when used during a transition will return the value for the state that is currently exiting. Since I was only looking at that value, the normalizedTime being reported was mixing in with the time of the Idle animation that was being blended with the attack.

If you want to check the animation that is being transitioned TO you have to use GetNextAnimatorStateInfo which only works during transitions. I altered the test script’s Update function to:

void Update() {
	AnimatorStateInfo currInfo = animator.GetCurrentAnimatorStateInfo(0);
	AnimatorStateInfo nextInfo = animator.GetNextAnimatorStateInfo(0);
		
	if(currInfo.IsTag("attack")) {
		Debug.Log(currInfo.normalizedTime);
	}
	if(nextInfo.IsTag("attack")) {
		Debug.Log(nextInfo.normalizedTime);
	}
}

Now this properly logs the normalizedTime for only the attack animation and doesn’t mix in the idle from the transitions, giving me much more consistent values to work with.

This doesn’t however fix the issue with reporting different values for different speeds. If I play the animation at normal speed the values are perfect, starting at 0 and ending at 1. If I play the animation at 1.5x speed, the values start at 0.1 and end at 1.1. At 2.0x speed they start at 0.16 and end at 1.22.

I’m pretty sure that I’m reading the correct values now so still confused why this is happening.

1 Like

Hello JonS, I know it’s probably a bit late to answer this problem, but I was wondering about this too. I think the problem might be that normalized time is not always from 0-1. it is only the fractional part (behind dot), which is percentage of the animation. The first decimal number is number of loop according to the documentation. So isn’t there a problem that with higher animation speed, the animation “overshoot” or goes to transitation and you get the number from other loop or so?

I will definitely try this… but if you already figured that out, let me know… thanks…

To keep it between 0-1 you can use:

Debug.Log(animator.GetCurrentAnimatorStateInfo(0).normalizedTime % 1)
2 Likes

I have just tested with my animators and it seems to be not just different speeds but is not reporting normalized time for the state at all. It seems to be reporting the overall local time of the animator instead of the local time of the state. Yup, it is kinda broken, as in reporting a totally different shit.

3 Likes