This is simple question. Have you ever seen AnimatorStateInfo’s normalizedTime actually got 0 (or very small value close to zero)? Today I set up some state machine and transitions and wrote following code to make sure the range of normalizeTime.
public IEnumerator DoAnimate()
{
animator.SetTrigger(nameOfAnimation);
isAnimating = true;
// wait for transition from current state to triggered state
while(animator.IsInTransition(0))
{
var stateInfo = animator.GetNextAnimatorStateInfo(0);
Debug.Log("DoAnimate: normalized time (in transition) = " + stateInfo.normalizedTime);
yield return null;
}
yield return null;
// triggered state is running
while(!animator.IsInTransition(0))
{
var stateInfo = animator.GetCurrentAnimatorStateInfo(0);
Debug.Log("DoAnimate: normalized time = " + stateInfo.normalizedTime);
yield return null;
}
// from triggered state to next state
while(animator.IsInTransition(0))
{
var stateInfo = animator.GetCurrentAnimatorStateInfo(0);
Debug.Log("DoAnimate: normalized time (in transition) = " + stateInfo.normalizedTime);
yield return null;
}
isAnimating = false;
}
[ContextMenu("Test")]
public void TestAnimation()
{
StartCoroutine(DoAnimate());
}
As you see, I used SetTrigger to transit the states. It works fine and the transition occurred. I experimented with various transition duration and Has Exit Time on/off (but transition offset = 0 whole the time and triggered state itself has its Exit Time). But for some reason first block checking the transition never runs (the while loop waiting the transition from previous state to triggered state).
I’ve never realized this whole the time. So, I decided to ask how it supposed to work. According to my understanding, the code should have worked in this way;
- After the trigger is fired, the state machine start to change its state from current state to triggered state.
- So, animator.IsInTransition(0) should be true, if the transition duration (set on inspector) is more than 0. In this case, it was. So I expected normalizeTime to be zero or at least very small value right after the transition (but it never happened).
- Then, after the transition complete, animator.IsInTransition(0) will return false (it did). At this point, the value of normalizedTime was already somewhere around 0.15 to 0.3 or so.
- The next transition occurs when the Animation clip (played in triggered state) reaches its Exit Time (it was somewhere around 0.8 or so in my case). animator.IsInTransition(0) returns true, third while loop prints normalizedTime until it gets 0.99 or so.
I also inserted some codes to wait one or several frames after SetTrigger. But it doesn’t do anything either. Maybe it’s caused by my settings or bugs I put in my code…but my animations seem to be played from the beginning!
The question is;
- Am I doing something wrong? Or is this how state machine supposed to work?
- If so, Are the any documents about some details like this?
- Have you every seen normalizedTime == 0 in your project?
The point is, my code is working, somehow. But I really couldn’t get myself comfortable without knowing such a basics…I thought I kinda understood how state machine works but it seems like there’s more for me to understand.
By the way, I know maybe I should use StateBehavior scripts to track the state but I still want to know how it works under the food.
Thank you for reading this long post.