Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. We're excited to be bringing you the newest Unity features, news, demos, and we've even got some special guests from some of the industry’s most storied franchises. Set a reminder to tune in on YouTube here
    Dismiss Notice
  3. Unity 2018.3 is now released.
    Dismiss Notice

Tips: Effective debugging for Mecanim State Machine

Discussion in 'Animation' started by Mecanim-Dev, Aug 19, 2014.

  1. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,626
    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.
     

    Attached Files:

  2. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    68
    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?
     
  3. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,626
    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

    recording.png playing.png
     
    StridingDragon likes this.
  4. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    68
    Ace! Thanks, I was being dense :) 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.
     
  5. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,626
    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
     
  6. Gamba

    Gamba

    Joined:
    Feb 8, 2015
    Posts:
    29
    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.
     
  7. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,626
    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
     
  8. Gamba

    Gamba

    Joined:
    Feb 8, 2015
    Posts:
    29
    That did it thanks! You mentioned that it was a bug, does that mean it has been fixed in one of the patch releases?
     
  9. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,626
    yes, it was fixed in 5.0.1 p1 which is not yet available.
     
  10. StridingDragon

    StridingDragon

    Joined:
    Jan 16, 2013
    Posts:
    71
    Awesome script! Thank you so much.
     
  11. jeffthoms321

    jeffthoms321

    Joined:
    Apr 26, 2017
    Posts:
    13
    Amazing and thank you unity for fixing the bug
     
  12. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    771
    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 :
    Code (CSharp):
    1.  
    2. void Update()
    3.     {
    4.      
    5.        if (m_Animator.recorderMode == AnimatorRecorderMode.Offline) StartRecord();
    6.         if(isRecording)
    7.         {              
    8.             if(samples.Count == (FrameCount-1)) // has looped, removed 1st sample
    9.             {                          
    10.                 samples.RemoveAt(0);                          
    11.             }
    12.             samples.Add(m_Animator.GetCurrentAnimatorStateInfo(0).nameHash);          
    13.         }          
    14.     }
     
    Mecanim-Dev likes this.