[Netcode] Client-Authoritative Aim Rotation to Eliminate Jitter
Solution
Unity 2022.3.x - Unity 6.3.x
Published Sat, May 9
In a networked game using Netcode for Entities, when both character movement and aim rotation are client-predicted, the AimRotation often exhibits jittering artifacts. This occurs when server corrections for rotation values conflict with local mouse input, causing the camera to snap back or stutter even when the owner prediction for position is functioning correctly. This creates a need for client-authoritative aim rotation for fluid visual feedback while maintaining server authority over world position.
To achieve responsive aim rotation while maintaining server-authoritative position, a specific networking architectural setup is required for your ghost prefab.
- Locate the
GhostAuthoringComponenton your ghost prefab. Set the serialization mode for theLocalTransformtoPosition Only. This ensures that position updates remain under server authority to prevent teleporting cheats, while allowing rotation to be handled separately. - Define a custom
IComponentDatastruct named AimRotation. Add the[GhostField]attribute to its fields (e.g.,PitchandYaw) to ensure the server broadcasts the finalized rotation to all proxy clients. - Create an
IInputComponentDatastruct to capture mouse deltas. On the owning client, process mouse input and update the AimRotation component immediately within thePredictedFixedStepSimulationSystemGroupfor instant visual feedback. - Within your movement system, extract the AimRotation from the input buffer. On the server, these values are used to determine the character’s forward vector for logic like projectile spawning, while the owner uses them for local camera orientation.
- Proxy clients (non-owners) receive the synchronized AimRotation values via the
GhostFieldand apply them to their local representation of the character to ensure the aiming direction is consistent across the network.
Additional Tips
- Use
quaternion.Eulerto convert the AimRotation pitch and yaw into a rotation for theLocalTransformto maintain compatibility with the Unity transform system. - Ensure the ghost collection is updated so that the AimRotation is included in the generated ghost code.
- If using a first-person camera, attach the camera to a child object that specifically reads the AimRotation pitch while the parent handles the yaw.
TL;DR
Configure the Ghost component for Position Only serialization on the server for predictive movement and manage AimRotation via client-driven input buffers to ensure responsive aiming.
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.