In this lesson, you will learn how to achieve visual parity across your multiplayer environment by using the NetworkAnimator component to sync complex state-machine transitions.
The Challenge of Distributed Animation
In a networked game, simply playing an animation locally on one client won't propagate that state to others. If you trigger a "Jump" animation solely via a local Animator.SetTrigger, other players will see your character model stuck in an idle pose while you float through the air. To maintain consistency, the state of the Animator must be replicated across the network so that every client knows exactly which animation clips are playing.
Leveraging NetworkAnimator
The NetworkAnimator component is the high-level tool provided by Netcode for GameObjects to handle this automatically. Instead of writing manual Remote Procedure Calls (RPCs) to trigger every animation transition, you attach this component to your networked prefab, which contains your Animator.
The NetworkAnimator observes the Animator component and serializes the state of the Animator Controller across the network. When a state change occurs (e.g., a parameter like isMoving changes to true), the NetworkAnimator captures this change and replicates the parameter value to all connected clients.
Implementation Setup
- Add Component: Attach
NetworkAnimatorto your player prefab alongside theNetworkObjectcomponent. - Assign Animator: Drag your character's
Animatorcomponent into the "Animator" field of theNetworkAnimatorinspector. - Parameter Syncing: In the
NetworkAnimatorsettings, you can define which parameters should be synchronized. While the component syncs most common states, ensuring your Animator Controller uses standardParameters(Triggers, Floats, Bools) is essential for the replication logic to function correctly.
// Inside your NetworkBehaviour
public void TriggerJump()
{
// By setting the parameter, the NetworkAnimator automatically
// replicates this state change to all clients.
myAnimator.SetTrigger("Jump");
}
By offloading this to the NetworkAnimator, you ensure that the complex state-driven logic in your Animator Controller remains perfectly synced, preventing the "ghosting" effects where player actions don't match their visual representation on remote machines.
Key Takeaway: The NetworkAnimator component abstracts the complexity of state replication, ensuring all clients observe the same animation state transitions by synchronizing Animator parameters across the network automatically.