Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NetworkAnimator SetTrigger host handling

Discussion in 'Netcode for GameObjects' started by Desoro, Mar 6, 2023.

  1. Desoro

    Desoro

    Joined:
    Apr 30, 2016
    Posts:
    9
    Code (CSharp):
    1. public void SetTrigger(int hash, bool setTrigger = true)
    2. {
    3.     if (!base.IsOwner && !base.IsServer)
    4.     {
    5.         return;
    6.     }
    7.  
    8.     AnimationTriggerMessage animationTriggerMessage = default(AnimationTriggerMessage);
    9.     animationTriggerMessage.Hash = hash;
    10.     animationTriggerMessage.IsTriggerSet = setTrigger;
    11.     AnimationTriggerMessage animationTriggerMessage2 = animationTriggerMessage;
    12.     if (base.IsServer)
    13.     {
    14.         m_NetworkAnimatorStateChangeHandler.QueueTriggerUpdateToClient(animationTriggerMessage2);
    15.         if (!base.IsHost)
    16.         {
    17.             InternalSetTrigger(hash);
    18.         }
    19.     }
    20.     else
    21.     {
    22.         m_NetworkAnimatorStateChangeHandler.QueueTriggerUpdateToServer(animationTriggerMessage2);
    23.         if (!IsServerAuthoritative())
    24.         {
    25.             InternalSetTrigger(hash);
    26.         }
    27.     }
    28. }
    What is the reasoning that hosts don't set triggers (NetworkAnimator) immediately? This is causing transition bugs where the delay is causing lower-priority transitions to execute when they shouldn't.

    Consider the following example:

    [Move => Attack]: a higher-priority transition that depends on a trigger
    [Move => Idle]: a lower-priority transition that depends on a bool

    As the bool change is immediate, and the trigger later, the animator will exit through [Move => Idle] causing a stutter, [Move => Idle => Attack].
     
    Last edited: Mar 6, 2023