Course contentsShow
← Unity Game Development
Lesson 800 of 82812. Multiplayer and DeploymentFree lesson

Resolving Conflicts with Server Reconciliation

Learners will implement logic to correct local client state when it diverges from the server's authoritative version of game events.

In this lesson, you will learn how to detect and correct state divergence by forcing the client to overwrite its local simulation with the server’s authoritative truth.

The Problem: Divergence

When you use Client-Side Prediction, your client moves immediately to provide a snappy feel. However, because the network is imperfect, the server might process your movement differently due to latency or environmental collisions. If the client’s predicted position drifts away from the server’s calculated position, the player will experience "rubber-banding." Reconciliation is the mechanism used to snap the client back to the correct path.

The Reconciliation Loop

To resolve these conflicts, we follow a three-step process:

  1. Tagging Inputs: Every time the client sends a movement input via ServerRpc, we store a unique ID (or timestamp) along with the local predicted state in a buffer.
  2. Verifying State: The server processes the movement and sends a ClientRpc back to the client containing the "Authoritative State" and the ID of the last processed input.
  3. Rewinding and Replaying: When the client receives the server's update, it checks the position. If the difference between the local prediction and the server state exceeds a small threshold, the client sets its position to the server's value. To prevent a jittery visual jump, the client then immediately "replays" all pending inputs that occurred after that ID to return to the current present moment.
void OnServerStateReceived(Vector3 serverPos, uint lastProcessedId) {
    // 1. Check if the local prediction matches the server
    if (Vector3.Distance(myPosition, serverPos) > threshold) {
        // 2. Snap to the authoritative truth
        transform.position = serverPos;
        
        // 3. Replay local inputs that the server hasn't seen yet
        ReplayPendingInputs(lastProcessedId);
    }
}

By re-running the physics calculations for all inputs that happened between the server's last update and now, we ensure the player stays in sync without losing the responsiveness of their recent inputs.

Key Takeaway: Reconciliation works by trusting the server as the absolute authority and rapidly re-simulating the local state whenever a discrepancy is detected.