Search Unity

Do StateMachineBehaviors on Layers interfere with SMBehaviors on States?

Discussion in 'Animation' started by tcz8, Jul 25, 2019.

  1. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    When I put a state machine behavior directly on a layer my character system gets messed up, as if the SMBehaviors on states from other layers stopped working!

    The script I put on the layer only outputs debug info, yet it creates this mess. I remove the script and BAM! it starts working again.

    Here is the script (Scroll pass the variables and you'll see it's quite simple):
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DebugEventOrder : StateMachineBehaviour {
    4.  
    5.     [Header("The label to output with the log")]
    6.     public string debugLabel;
    7.     [Header("Select which events to log")]
    8.     public bool logAwake = true;
    9.     public bool logOnDestroy = true;
    10.     public bool logOnDisable = true;
    11.     public bool logOnEnable = true;
    12.     public bool logOnStateMachineEnter = true;
    13.     public bool logOnStateMachineExit = true;
    14.     public bool logOnStateEnter = true;
    15.     public bool logOnStateExit = true;
    16.     public bool logOnStateUpdate = true;
    17.     public bool logOnStateMove = true;
    18.     public bool logOnStateIK = true;
    19.     [Header("The number of times logged events are called")]
    20.     public int awakeCount = 0;
    21.     public int onDestroyCount = 0;
    22.     public int onDisableCount = 0;
    23.     public int onEnableCount = 0;
    24.     public int onStateMachineEnter = 0;
    25.     public int onStateMachineExit = 0;
    26.     public int onStateEnter = 0;
    27.     public int onStateExit = 0;
    28.     public int onStateUpdate = 0;
    29.     public int onStateMove = 0;
    30.     public int onStateIK = 0;
    31.  
    32.     void Awake () {
    33.         // Reset counters
    34.         awakeCount = 0;
    35.         onDestroyCount = 0;
    36.         onDisableCount = 0;
    37.         onEnableCount = 0;
    38.         onStateMachineEnter = 0;
    39.         onStateMachineExit = 0;
    40.         onStateEnter = 0;
    41.         onStateExit = 0;
    42.         onStateUpdate = 0;
    43.         onStateMove = 0;
    44.         onStateIK = 0;
    45.  
    46.         if (logAwake) {
    47.             awakeCount++;
    48.             Debug.Log("SMB TESTER: Awake   " + debugLabel);
    49.         }
    50.     }
    51.  
    52.     void OnDestroy () {
    53.         if (logOnDestroy) {
    54.             onDestroyCount++;
    55.             Debug.Log("SMB TESTER: OnDestroy   " + debugLabel);
    56.         }
    57.     }
    58.  
    59.     void OnDisable () {
    60.         if (logOnDisable) {
    61.             onDisableCount++;
    62.             Debug.Log("SMB TESTER: OnDisable   " + debugLabel);
    63.         }
    64.     }
    65.  
    66.     void OnEnable () {
    67.         if (logOnEnable) {
    68.             onEnableCount++;
    69.             Debug.Log("SMB TESTER: OnEnable   " + debugLabel);
    70.         }
    71.     }
    72.  
    73.     void OnStateMachineEnter () {
    74.         if (logOnStateMachineEnter) {
    75.             onStateMachineEnter++;
    76.             Debug.Log("SMB TESTER: OnStateMachineEnter   " + debugLabel);
    77.         }
    78.     }
    79.  
    80.     void OnStateMachineExit    () {
    81.         if (logOnStateMachineExit) {
    82.             onStateMachineExit++;
    83.             Debug.Log("SMB TESTER: OnStateMachineExit   " + debugLabel);
    84.         }
    85.     }
    86.  
    87.     override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    88.         if (logOnStateEnter) {
    89.             onStateEnter++;
    90.             Debug.Log("SMB TESTER: OnStateEnter   " + debugLabel);
    91.         }
    92.     }
    93.  
    94.     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    95.         if (logOnStateExit) {
    96.             onStateExit++;
    97.             Debug.Log("SMB TESTER: OnStateExit   " + debugLabel);
    98.         }
    99.     }
    100.  
    101.     override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    102.         if (logOnStateUpdate) {
    103.             onStateUpdate++;
    104.             Debug.Log("SMB TESTER: OnStateUpdate   " + debugLabel);
    105.         }
    106.     }
    107.  
    108.     override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    109.         if (logOnStateMove) {
    110.             onStateMove++;
    111.             Debug.Log("SMB TESTER: OnStateMove   " + debugLabel);
    112.         }
    113.     }
    114.  
    115.     override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    116.         if (logOnStateIK) {
    117.             onStateIK++;
    118.             Debug.Log("SMB TESTER: OnStateIK   " + debugLabel);
    119.         }
    120.     }
    121. }
     
    Last edited: Jul 25, 2019
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    It wouldn't surprise me at all to learn that the stupid Animator Controller system has yet another stupid quirk or bug. I can't see any way that script should affect the behaviour of anything else unless another script is directly accessing its public fields or something, which I'm guessing isn't the case.

    You might be interested in my Animancer plugin (link in my signature) which lets you avoid using Animator Controllers. It comes with its own State Machine system (with examples) which is a lot more flexible than Animator Controllers.
     
  3. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Unfortunatly there is no way I am moving 12 layers of animations, our weapon, inventory and character system including all the code for our parkour system to another animation tool.

    Will give it a look before starting our next project.
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    You have my condolences. That must be hell to deal with even when it's working properly.
     
  5. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Works well so far, except for that weird problem. It helps to have a senior animator on board.
     
    Last edited: Jul 28, 2019