In this lesson, you will learn how to synchronize animations that move the character’s capsule to ensure the server and client remain in perfect alignment.
The Challenge of Root Motion
When you use Root Motion, the animation itself dictates the character's movement, extracting translation data directly from the mesh’s root bone. In a networked environment, this poses a synchronization nightmare: if the client plays a 2-second leap animation, the server must calculate the exact same displacement. If the client’s frame rate dips or packet loss occurs, the client and server positions will drift, triggering the CharacterMovementComponent to force a reconciliation, causing the dreaded "rubber-banding."
Configuring Root Motion for Networking
To handle this, you must offload the root motion calculations to the CharacterMovementComponent. Instead of just playing an animation, you tell the component to process the root motion data through the standard movement pipeline.
- Enable Root Motion: In your
UAnimMontage, set "Root Motion Mode" toRoot Motion from Everything. - Server-Authoritative Processing: You must ensure the
CharacterMovementComponentis aware of the root motion. When usingPlayMontageAndWaitorPlayAnimMontage, the component hooks into the movement flow. - The Replication Hook: Set
bAllowPhysicsRotationDuringAnimRootMotionor rely on theRootMotionSourcesystem. By usingRootMotionSource, you explicitly define movement patterns that the server can track and replicate as part of theSavedMovedata during client-side prediction.
// Within your Character class, ensure movement reflects the animation
void AMyCharacter::PerformRootMotionAttack()
{
// Ensure the Server initiates the montage so clients sync correctly
if (HasAuthority())
{
MulticastPlayAttackMontage();
}
else
{
ServerPlayAttackMontage();
}
}
The key is that the server must execute the same animation logic. Because the CharacterMovementComponent tracks RootMotionSource during its simulation ticks, the server validates the movement frame-by-frame. If the client tries to "cheat" or desync by playing an animation that moves them through a wall, the server’s authoritative simulation will override the client’s position during the reconciliation process.
Key Takeaway: Root motion in networked games must be processed through the CharacterMovementComponent’s authoritative simulation pipeline to ensure the server validates and replicates movement displacement rather than just animation playback.