UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

architecture

[Netcode] Client-Authoritative Aim Rotation to Eliminate Jitter

Solution

multiplayeroptimizationinputnetcodeuser experience

Unity 2022.3.x - Unity 6.3.x

Published Sat, May 9

Issue

 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.

Explanation

To achieve responsive aim rotation while maintaining server-authoritative position, a specific networking architectural setup is required for your ghost prefab.

  1. Locate the GhostAuthoringComponent on your ghost prefab. Set the serialization mode for the LocalTransform to Position Only. This ensures that position updates remain under server authority to prevent teleporting cheats, while allowing rotation to be handled separately.
  2. Define a custom IComponentData struct named AimRotation. Add the [GhostField] attribute to its fields (e.g., Pitch and Yaw) to ensure the server broadcasts the finalized rotation to all proxy clients.
  3. Create an IInputComponentData struct to capture mouse deltas. On the owning client, process mouse input and update the AimRotation component immediately within the PredictedFixedStepSimulationSystemGroup for instant visual feedback.
  4. 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.
  5. Proxy clients (non-owners) receive the synchronized AimRotation values via the GhostField and apply them to their local representation of the character to ensure the aiming direction is consistent across the network.

Additional Tips

  • Use quaternion.Euler to convert the AimRotation pitch and yaw into a rotation for the LocalTransform to 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.