Tips: Effective debugging for Mecanim State Machine

Hi Guys,

As a Mecanim developper, one of our job is to investigate and fix bug that you guys found. Sometime the bug is not in code itself but in your game script code that setup the animation system or in the State Machine. The visual animator tool coupled with the live link feature is really great to debug your state machine but sometime it simply goes to fast and you don’t see what going on.

Here a small script that use the Animator recording service that can help you track down these issues.
Put this script on you Animator game object and it will start to record everything that the Animator does
When you hit a bug, just press the pause button to start the playback mode, open the animator tool and select your animator gameobject to start live link.
Once in this mode you can play frame by frame or go to a specific frame with the slider and the tools should show you all your variables values and update the state machine to the current state of the animation.

1740632–109978–Recorder.cs (2.05 KB)

15 Likes

This sounds awesome but I can’t actually see how to use it. I’ve attached the Recorder script to the same GO as my Animator and ‘Is Recording’ is ticked. However when I pause simulation mode and go to the animator window, I don’t see any additional tools or buttons to control the frame by frame playback. This is with Unity5, any chance you could show a quick visual guide incase I’m being particularly dense?

All the custom UI stuff should appear in your game view at the top of the window.

when playing back what you did record, you have a few button to control the playback.
go to previous frame, go to next frame. Pressing the play button return to record mode.
and you have slider to move the current frame time

1 Like

Ace! Thanks, I was being dense :slight_smile: Works perfectly.

The only issue I can immediately see is that if trigger parameters are acted upon per frame, it’s not possible to easily debug / track these through the frame steps as you can’t see the actual paramter being set unless it’s not acted upon until a different frame. This is a minor problem I guess as it requires a transition with no state exit time based on the trigger (and thus you could track it based on what transition logic occured) however would be a nice to have I guess.

Right, trigger can be tricky to debug if they are set and consumed in the same frame but you will see the state change in the graph view so you can assume it was set and consumed right away.

Also this solution is not perfect, it doesn’t work with physic simulation very well

Hi,

I have a different kind of problem with the mecanim state machines. The [SharedBetweenAnimators] attribute seems to be working even when I don’t add it to my behaviour scripts and so I can’t get instanced versions on multiple states. Any ideas?

Thanks.

We had a bug in 5.0, if you copy paste a state, statemachine or controller, SMB instances are not cloned they are only referenced.

Select one of the problematic state, delete your ‘shared’ SMB and create a new one.

If it doesn’t seem to be this issue then you could log a bug, send me your case number when you get it.

best regards

That did it thanks! You mentioned that it was a bug, does that mean it has been fixed in one of the patch releases?

yes, it was fixed in 5.0.1 p1 which is not yet available.

Awesome script! Thank you so much.

Amazing and thank you unity for fixing the bug

Has this been integrated or it’s still needed?
By the way I made some change, in case somebody is using playback mode the recorder will stop and never restart. This way it always check if it is recording and restart if possible :

void Update()
    {
      
       if (m_Animator.recorderMode == AnimatorRecorderMode.Offline) StartRecord();
        if(isRecording)
        {              
            if(samples.Count == (FrameCount-1)) // has looped, removed 1st sample
            {                          
                samples.RemoveAt(0);                          
            }
            samples.Add(m_Animator.GetCurrentAnimatorStateInfo(0).nameHash);          
        }          
    }
3 Likes

I had this same issue this morning. What I had to do was, in the state(s) being transitioned to, locate the box that says something like “can transition to itself” and uncheck it.

This is a minor issue I surmise as it requires progress with no state leave time in light of the trigger (and accordingly you could follow it in view of what change rationale occurred) but would be great to have I presume.

Great script! You played it like a champ!!!
And most importantly the problem is behind us.