Lag Compensation
With interpolation, you see other players where they were about 100 ms ago, plus half your RTT. Aim perfectly at an enemy running sideways and fire: by the time the shot reaches the server, the enemy is somewhere else. Without help, players at any real ping would have to lead their targets by an amount that changes with their ping, which nobody can do. Lag compensation fixes this on the server: when a shot arrives, the server rewinds the other players to where the shooter saw them, checks the hit there, and then puts everything back.
It was popularized by Valve's Half-Life and Source engines and is standard in competitive shooters.
What the server needs
- A history of every player's hitboxes for the last few hundred milliseconds: one entry per tick, in a ring buffer.
- The time the shooter was looking at: the shot carries the client's tick and its interpolation state (which two snapshots and the blend fraction), or the server computes it from the client's latency and interpolation delay.
- A limit: never rewind more than, say, 200–250 ms. Without one, a player with a 1-second lag spike could hit people who had been safely behind cover for ages.
Rewinding, step by step
A target runs right at 10 units per tick (at 20 Hz). The shooter has 100 ms RTT and a 100 ms interpolation delay, so the target they see is 3 ticks old (50 ms one way + 100 ms of interpolation = 150 ms). They aim exactly at the target as drawn and fire. The server checks the hit twice, once against the present and once rewound:
const TICK_MS = 50;
const HALF_WIDTH = 6; // target hitbox: x ± 6
const targetX = (tick: number) => 100 + tick * 10;
// Server history: target position per tick (a real server keeps whole hitbox sets).
const pastX = new Map<number, number>();
for (let t = 0; t <= 40; t++) pastX.set(t, targetX(t));
function hitTest(aimX: number, tx: number) { return Math.abs(aimX - tx) <= HALF_WIDTH; }
function serverCheck(shotArrivesTick: number, clientViewTick: number, maxRewindMs: number) {
const now = targetX(shotArrivesTick);
const aim = targetX(clientViewTick); // the shooter aimed exactly at what they saw
const rewindMs = (shotArrivesTick - clientViewTick) * TICK_MS;
const allowed = rewindMs <= maxRewindMs;
const past = allowed ? pastX.get(clientViewTick)! : now;
console.log(`shot aimed at x=${aim} (what the client saw, tick ${clientViewTick}); arrives at tick ${shotArrivesTick} when target is at x=${now}`);
console.log(` no compensation: ${hitTest(aim, now) ? 'HIT' : 'MISS'} (off by ${now - aim})`);
console.log(` rewind ${rewindMs} ms ${allowed ? '' : `(over the ${maxRewindMs} ms limit, not allowed) `}-> target at x=${past}: ${hitTest(aim, past) ? 'HIT' : 'MISS'}`);
}
// Shot fired at server tick 20 (client view = tick 17), arrives one tick later (50 ms one way).
serverCheck(21, 17, 250);
// A player with a 900 ms lag spike: their view is 20 ticks old.
serverCheck(30, 10, 250);shot aimed at x=270 (what the client saw, tick 17); arrives at tick 21 when target is at x=310
no compensation: MISS (off by 40)
rewind 200 ms -> target at x=270: HIT
shot aimed at x=200 (what the client saw, tick 10); arrives at tick 30 when target is at x=400
no compensation: MISS (off by 200)
rewind 1000 ms (over the 250 ms limit, not allowed) -> target at x=400: MISS
Without compensation the shooter misses a target they aimed at perfectly, by 40 units. With rewind, the server sees exactly what the shooter saw and the shot hits. The second shot shows why the limit exists: a view 1000 ms old is not rewound.
Interpolating the history
Clients render between snapshots, so the shooter's view time usually falls between two server ticks. A precise server stores the client's interpolation fraction with the shot and blends the two history entries the same way, so the rewound hitbox matches the drawn model to the millimetre:
using System;
// Hitbox centre per server tick, as stored in the history buffer.
double[] history = { 100, 110, 120, 130, 140 };
// The client drew the target 40% of the way from tick 2 to tick 3.
int fromTick = 2; double fraction = 0.4;
double drawnX = history[fromTick] + (history[fromTick + 1] - history[fromTick]) * fraction;
double aim = drawnX; // perfect aim at the drawn model
foreach (var (name, x) in new[] { ("whole tick 2", history[2]), ("whole tick 3", history[3]), ("interpolated", drawnX) })
{
double off = Math.Abs(aim - x);
Console.WriteLine(quot;{name,-13} target x={x,6:F1} off by {off,4:F1} {(off <= 3 ? "HIT" : "MISS")} (hitbox ±3)");
}whole tick 2 target x= 120.0 off by 4.0 MISS (hitbox ±3)
whole tick 3 target x= 130.0 off by 6.0 MISS (hitbox ±3)
interpolated target x= 124.0 off by 0.0 HIT (hitbox ±3)
The trade-off: "I got shot behind the wall"
Lag compensation is fair to the shooter: what you see is what you hit. The victim pays: the server judges their position as it was some 150–250 ms ago, so a player who has just ducked behind cover can still be hit by someone who saw them a moment earlier. With low pings nobody notices; when the shooter has a high ping, the victim does. Games limit the damage:
- Cap the rewind (the 200–250 ms above); high-ping players must lead their shots beyond that.
- Don't compensate everything: slow projectiles (rockets, grenades) are usually simulated on the server in real time, not rewound; only instant hitscan shots are.
- Peeker's advantage is the related effect from the other side: a player stepping around a corner sees the defender before the defender's client has received the peeker's new position. Higher tick rates and lower interpolation delays shrink it; nothing removes it.
Implementation notes
- Rewind only the players that matter: those near the shot's ray (a broad-phase check against current positions with a margin), not the whole world.
- Rewind hitboxes, not physics: move the collision shapes, test the ray, restore them. Don't run game logic in the rewound state.
- Validate the claimed view time: never trust a client that says "I was looking at 2 seconds ago". Compute it from the server's own estimate of that client's latency, or clamp it (Never Trust the Client).
- Engines differ: Unreal and Unity leave lag compensation to the game (or plugins), while some frameworks (Photon Fusion, for example) provide lag-compensated hitbox queries out of the box (Mirror, FishNet & Photon Fusion).
Test yourself
What does the server do when a lag-compensated shot arrives?
- Delays the shot by the shooter's ping
- Rewinds other players' hitboxes to where the shooter saw them, tests the hit, then restores them
- Asks the victim's client whether it was hit
- Extrapolates the shooter forward
Why do servers cap how far back they will rewind?
- Memory is limited to one tick
- To stop high-latency or lagging players from hitting targets that have long been safe behind cover
- Rewinding is illegal
- To save bandwidth
Who pays the price of lag compensation?
- The shooter
- The victim, who can be hit shortly after reaching cover
- The server's bandwidth
- Nobody