Unity's NetCode multiplayer online game online battle tutorial (9)--NetworkAnimator component

Preface

This component is one of the commonly used components in NetCode. NetworkAnimator is the same as NetworkTransform. It is used to synchronize information. NetworkAnimator It is used to synchronize animations.


NetworkAnimator

NetworkAnimatorThe component provides a basic example of how to synchronize animations during a network session. The animation state is synchronized with players joining an existing network session, and with any clients that were connected before the animation state changed.

Players joining an existing network session will be synchronized with:

  • Possessive Animator's Obvious attribute Su Status .
    • Except Animatortrigger property. These are only synchronized with connected clients.
  • any ongoing transition
  • Connected players will be synchronized with Animator changes:
    • state
    • transition
    • Attributes
      • NetworkAnimator only synchronizes property values ​​that have changed sincethe previous frame.
      • Sincetrigger is similar toevent, when the Animator property is set to true, it will always be synchronized.

NetworkAnimatorCan run in two authoritative modes:

  • Server Authoritative (default): The server initiates animated state changes.
    • The owner can still callNetworkAnimator.SetTrigge.
  • Client Authority: The client owner initiates animated state changes.

Animator's Trigger property

Animator'sTrigger attribute type is basically nothing more than aBoolean value, when set to When true, the Animator component will automatically reset to false after the component handles the trigger. a>. Typically, triggers are used to initiate transitions between Animator layer states. In this sense, Trigger can be seen as a way of signaling the "beginning of an event". Because the Triggerproperty has this unique behavior, the trigger value is set via the NetworkAnimator.SetTrigger method.

Note: Thiswill notSynchronizewith non-owner clients. Animator.SetTriggerTrigger


Server Authoritative Mode

NetworkAnimatorThe default setting for is服务器权威模式(Server Authoritative Mode). In server authoritative mode, any animation state changes that are set (triggers) or detected (changes to layers, states, or any Animator properties, excluding triggers) on the server side will be synchronized with all clients. Since the server initiates any synchronization of Animator state changes, the owner client of the NetworkObject associated with the NetworkAnimator may lag by approximately a full round trip time (RTT). Here is a timing diagram to demonstrate this:


In the diagram above, the client might send an RPC to the server telling the server that the player is performing some action (including setting a trigger) that might change the player's animation. In this case, the client sends an RPC (half RTT) to the server, the server handles the RPC, the NetworkAnimator (server side) detects the relevant Animator state change, and then all clients (including the owner client) synchronize with the change .

  • Advantages of the server authority model:
    • If running a regular server (non-host), this model helps reduce synchronization delays between all client animations.
  • Disadvantages of the server authority model:
    • The host will always be "slightly ahead" of all other clients, which may or may not be an issue for your project.
    • Client owners will experience delays when performing actions (moving, picking up items, anything that causes the Animator state to change).

Client Authoritative Mode (Owner Authoritative Mode)

Often, project design (or personal preference) may require the owner to update immediately to any Animator state changes. The most typical reason is to provide immediate visual (animation) feedback to local players. To create a NetworkAnimator with the owner as the authority, you need to create a new class that derives from NetworkAnimator, overriding < /span> method, as in the example provided below:NetworkAnimator.OnIsServerAuthoritative method and return false in the overridden OnIsServerAuthoritative

using UnityEngine;
using Unity.Netcode.Components;

public class OwnerNetworkAnimator : NetworkAnimator
{
    
    
    protected override bool OnIsServerAuthoritative()
    {
    
    
        return false;
    }
}

This writing method is the same as the previous oneClientNetworkTransform.

Observing the timings of the NetworkAnimator where the owner is authoritative, in the chart below you can see that although the owner client gets an "immediate visual animation response", the non-owner client ends up lagging behind the owner client by about one The full round trip time (RTT), while the host will lag behind the owner client for half the RTT.

In the diagram above, it is shown that the owner client has an Animator state change, which is detected byNetworkAnimator(OwnerNetworkAnimator), which automatically synchronizes the server with the changed state. The server applies these changes locally and then broadcasts this state change to all non-owner clients.

  • Owner authoritative mode优点
    • Owners caninstantly get visual feedback of Animator state changes, providing a smoother experience for local players.
  • Owner authoritative mode completed
    • The non-owner client's animation lagsthe owner client by approximately one full round trip time (RTT).
    • The host's animation lagsthe owner client's animation by approximately half a round trip time (RTT).

Study documents

https://docs-multiplayer.unity3d.com/netcode/current/components/networkanimator/

Guess you like

Origin blog.csdn.net/a924282761/article/details/134430967