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.