🐾PurrNet Docs
PurrDiction

Client-Side Prediction

PurrDiction is a separate PurrNet module that adds client-side prediction and server reconciliation. The client acts immediately on input while the server remains authoritative — divergences are corrected with a seamless snap.

PurrDiction — prediction & reconciliation
PredictedServer
S
P
Divergence: 0.0px
Threshold: 18px
Reconciliations: 0

What is PurrDiction?

Standard NetworkTransform introduces a delay equal to your round-trip time before the player sees the result of their own input. At 100 ms ping this is noticeable; at 200 ms it feels broken. PurrDiction eliminates this by running the game simulation locally on the client before the server confirms it, then correcting any errors when the authoritative result arrives.

PurrDiction is a separate Unity package built on top of PurrNet. Install it alongside PurrNet; it does not replace any existing functionality.

Core concepts

  • Input prediction: The client applies input immediately to a local simulation. The player sees responsive movement with zero perceived latency.
  • Server simulation:The server receives the same input stream (with a tick timestamp) and simulates the same logic. The server's result is authoritative.
  • Reconciliation:When the server's acknowledged state arrives, the client compares it to its own history. If the difference exceeds ReconcileThreshold, the client rewinds to the server state and replays all unacknowledged inputs forward — producing a seamless correction.

Key classes

ParameterTypeDefaultDescription
PredictedIdentityclass—Base class for any networked object that participates in prediction. Replaces NetworkBehaviour. Manages the rollback buffer and reconciliation loop.
PredictedModuleclass—Module equivalent of PredictedIdentity — attach to a PredictedIdentity field for modular prediction logic (movement, abilities, etc.).
InputStateabstract struct : IPackedAuto—Your input snapshot for one tick. Implement this with your movement axes, jump flags, aim direction, etc.
WorldStateabstract struct : IPackedAuto—Your character's authoritative state snapshot. Position, velocity, health — anything the server simulates. Must be byte-comparable for reconciliation.
ReconcileThresholdfloat—Maximum allowed difference (world units) between predicted and server state before a reconciliation snap is triggered.

The prediction loop

  1. Each tick the owner samples input and stores it in a ring buffer.
  2. Input is applied locally to advance the character state (client-side).
  3. Input is sent to the server along with the current tick number.
  4. The server simulates the same input and broadcasts the resulting state.
  5. The client receives the server state and compares it to the buffer entry for that tick.
  6. If the difference exceeds the threshold, the client rewinds to the server state and re-simulates all ticks from that point forward using the buffered inputs.

Minimal predicted character controller

PredictedCharacter.cs
using PurrNet;
using PurrDiction;
using UnityEngine;

// InputState: sampled once per tick on the owner
[RegisterNetworkType(typeof(CharacterInput))]
public struct CharacterInput : IPackedAuto
{
    public Vector2 moveAxis;  // WASD / left stick
    public bool    jump;
    public float   aimYaw;    // horizontal look angle
}

// WorldState: authoritative snapshot — must be byte-comparable
[RegisterNetworkType(typeof(CharacterState))]
public struct CharacterState : IPackedAuto
{
    public Vector3 position;
    public Vector3 velocity;
    public bool    isGrounded;
}

public class PredictedCharacter : PredictedIdentity<CharacterInput, CharacterState>
{
    [SerializeField] private CharacterController _cc;
    [SerializeField] private float               _moveSpeed = 5f;
    [SerializeField] private float               _jumpForce = 6f;

    private Vector3 _velocity;

    // --- Override: gather input from the local player ---
    protected override CharacterInput GatherInput()
    {
        return new CharacterInput
        {
            moveAxis = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")),
            jump     = Input.GetButtonDown("Jump"),
            aimYaw   = Camera.main.transform.eulerAngles.y,
        };
    }

    // --- Override: simulate one tick given an input (runs on client AND server) ---
    protected override void SimulateTick(CharacterInput input, float dt)
    {
        Vector3 move = new Vector3(input.moveAxis.x, 0, input.moveAxis.y);
        move = Quaternion.Euler(0, input.aimYaw, 0) * move * _moveSpeed;

        if (_cc.isGrounded)
        {
            _velocity.y = input.jump ? _jumpForce : -0.5f;
        }
        else
        {
            _velocity.y += Physics.gravity.y * dt;
        }

        _cc.Move((move + new Vector3(0, _velocity.y, 0)) * dt);
    }

    // --- Override: capture current state snapshot ---
    protected override CharacterState CaptureState()
    {
        return new CharacterState
        {
            position   = transform.position,
            velocity   = _velocity,
            isGrounded = _cc.isGrounded,
        };
    }

    // --- Override: apply a state snapshot (used during reconciliation) ---
    protected override void ApplyState(CharacterState state)
    {
        // Teleport to the authoritative position without interpolation
        Physics.SyncTransforms();
        transform.position = state.position;
        _velocity          = state.velocity;
        Physics.SyncTransforms();
    }
}

When to use PurrDiction

Good candidates

  • First-person and third-person shooters — responsive movement is critical
  • Platformers — jump timing must feel instant
  • Racing games — steering and acceleration must not feel delayed
  • Battle royale / competitive games with low tolerance for latency feel

Poor candidates

  • Turn-based strategy — no continuous input; prediction adds complexity for no benefit
  • RPGs with complex server-side state (stats, quests, economy) — reconciliation is expensive
  • Puzzle games or card games — latency is irrelevant; use standard RPCs
  • Simple mobile games — NetworkTransform with interpolation is usually good enough

Prediction adds significant complexity

PurrDiction requires every game system that affects the predicted character — movement, physics, abilities, collision — to be written as a deterministic, tick-based simulation that runs identically on client and server. Retrofitting an existing codebase can be a large effort. Start with standard NetworkTransform and add prediction only when you have measured that latency is a real problem for your players.

Start with NetworkTransform first

For most indie games, NetworkTransform with interpolation is perfectly acceptable. Players tolerate ~100 ms of movement lag in cooperative games. Only reach for PurrDiction when building a competitive game where prediction error means losing a fight.

PurrDiction is a separate package

Install PurrDiction from the Package Manager using its separate Git URL or UPM package ID. It depends on PurrNet but does not ship with it. Check the PurrNet Discord or GitHub for the latest release tag.