When engineering competitive games like WRX Battles Online, latency is your greatest adversary. Even with fiber connections, round-trip times (RTT) range from 20ms to 120ms. If you wait for the server to acknowledge an input before moving the player, the game will feel heavy, sluggish, and unplayable.

To deliver an instantaneous, esports-grade responsiveness while maintaining absolute server authority against cheaters, we employ the trinity of modern game networking: Client-Side Prediction, Server Reconciliation, and Snapshot Interpolation.

1. The Physics Paradox in Real-Time Games

Why can't clients simply transmit their transform.position across the network?

  • Cheating & Speed Hacks: If the server trusts client positions, a modified client can teleport across the map or ignore walls.
  • Physics Non-Determinism: Floating-point calculations slightly vary across different CPUs, causing simulations to desynchronize over time.
The Golden Rule of Game Networking
Never trust the client. The client only sends inputs and commands (e.g., "AccelerateForward", "SteerAngle: 0.4"). The authoritative server executes the simulation and broadcasts the definitive world state.

2. Client-Side Prediction: Eliminating Input Lag

Instead of waiting for the server's reply, the local client applies inputs immediately to its local simulation. Simultaneously, it tags each input with an incremental tick number and saves it into a circular history buffer:

ClientPredictionSystem.cs
using System.Collections.Generic;
using UnityEngine;

public struct PlayerInputPayload
{
    public uint Tick;
    public float Throttle;
    public float Steering;
    public bool Handbrake;
}

public struct ServerStatePayload
{
    public uint AckTick;
    public Vector3 Position;
    public Quaternion Rotation;
    public Vector3 Velocity;
}

public class ClientPredictionSystem : MonoBehaviour
{
    private const int BUFFER_SIZE = 1024;
    private readonly PlayerInputPayload[] _inputBuffer = new PlayerInputPayload[BUFFER_SIZE];
    private readonly Vector3[] _positionBuffer = new Vector3[BUFFER_SIZE];

    private uint _currentTick = 0;
    private Rigidbody _rb;

    private void Awake()
    {
        _rb = GetComponent<Rigidbody>();
    }

    public void OnFixedUpdateTick(float throttle, float steering, bool brake)
    {
        _currentTick++;
        uint bufferIndex = _currentTick % BUFFER_SIZE;

        // 1. Record input payload
        PlayerInputPayload input = new PlayerInputPayload
        {
            Tick = _currentTick,
            Throttle = throttle,
            Steering = steering,
            Handbrake = brake
        };
        _inputBuffer[bufferIndex] = input;

        // 2. Transmit to Authoritative Server via UDP socket
        NetworkTransport.SendInputToServer(input);

        // 3. Immediately predict local motion
        SimulatePhysicsLocally(input);
        _positionBuffer[bufferIndex] = transform.position;
    }

    // Called whenever an authoritative state arrives from server
    public void OnServerStateReceived(ServerStatePayload serverState)
    {
        uint bufferIndex = serverState.AckTick % BUFFER_SIZE;
        Vector3 predictedPos = _positionBuffer[bufferIndex];

        // Measure error divergence threshold
        float error = Vector3.Distance(predictedPos, serverState.Position);
        if (error > 0.05f) // Significant drift detected (packet loss or collision)
        {
            ReconcileServerDivergence(serverState);
        }
    }

    private void ReconcileServerDivergence(ServerStatePayload authoritativeState)
    {
        // Snap back to authoritative state
        transform.position = authoritativeState.Position;
        transform.rotation = authoritativeState.Rotation;
        _rb.velocity = authoritativeState.Velocity;

        // Replay all inputs from the acknowledged tick up to current tick!
        uint replayTick = authoritativeState.AckTick + 1;
        while (replayTick <= _currentTick)
        {
            uint replayIndex = replayTick % BUFFER_SIZE;
            SimulatePhysicsLocally(_inputBuffer[replayIndex]);
            _positionBuffer[replayIndex] = transform.position;
            replayTick++;
        }
    }

    private void SimulatePhysicsLocally(PlayerInputPayload input)
    {
        Vector3 force = transform.forward * (input.Throttle * 1500f);
        _rb.AddForce(force * Time.fixedDeltaTime, ForceMode.Force);
        transform.Rotate(0f, input.Steering * 60f * Time.fixedDeltaTime, 0f);
    }
}

3. Server Reconciliation: Resolving Divergence

When an authoritative packet arrives, the client compares the predicted position against the server's computed position. If divergence exceeds a tiny threshold (e.g. 5 centimeters due to another vehicle colliding with you), the client snaps to the server's snapshot and replays all unacknowledged inputs in a single frame. To the player, corrections feel seamless without teleportation snaps.

4. Entity Interpolation for Remote Opponents

For other players on the field, we don't predict their inputs. Instead, we buffer server states by approximately 50-100ms and smoothly interpolate (LERP / SLERP) between the two most recent snapshots. This creates fluid motion even during packet jitter.

5. Rewind Hit-Registration & Anti-Cheat Bounds

When a sniper rifle or cannon fires in WRX Battles Online, the projectile hit is calculated using Lag Compensation:

  1. The client fires at a target that was rendered 80ms in the past due to interpolation delay.
  2. The server receives the shoot command with the client's timestamp.
  3. The server rewinds all player hitboxes back 80ms into its historical snapshot ring buffer.
  4. The server executes the raycast hit test. If valid, damage is awarded.
  5. The server snaps hitboxes back to present time.