Inconsistency in StateMachineBehaviour?

The new tutorial for StateMachineBehaviours says:

  • The AnimatorStateInfo is the current info for the state that the state machine behaviour is on. It is the equivalent of writing animator.GetCurrentStateInfo(layerIndex); This can be useful in operations that involve the normalised time of the clip.

I’ve noticed that this isn’t always true. In my project, the following code prints the debug message up to 12 frames in a row:

    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (animator.GetCurrentAnimatorStateInfo(layerIndex).shortNameHash != stateInfo.shortNameHash)
            Debug.Log("Inconsistent AnimatorStateInfos...");
    }

Unfortunately, this is in a project from which I’m not currently able to file bug reports. But it seems to me that this is a small bug that should be easy to find and fix.

Create a new project, create a dummy animator and state machine behaviour, and submit as bug report… for fastest fix.

maybe message @Mecanim-Dev

i was unable to reproduce the issue, might have been due to the simplicity of my scene and only one animation

this is the scene I tried with

Thx WendelingReich for pointing out this issue

We will do some investigation and see if we can repro on our side too.

Best regards,

2 Likes

After further investigation this is expected, you cannot use animator.GetCurrentAnimatorStateInfo like this. Here the reason

Let say you have a controller with 2 State A and B with a transition from A to B and you have a StateMachineBehaviour (SMB) on state B only. When your SMB will start to get is first callback OnStateEnter, it does mean that the StateMachine is doing a transition between A to B, so in this case B is the next state, to correctly validate that both AnimatorStateInfo are equivalent you should use animator.GetNextAnimatorStateInfo.

This is exactly why we did add AnimatorStateInfo as a parameter to all callback. Remove some complexity to handle such case.

Best regards

I see. So parameter ‘stateInfo’ always refers to the state to which ‘this’ SMB is attached?

Yes, basically just before we do call your callback we do query the StateInfo for you and pass it as a parameters to the callback.