Lag Compensation in Unreal Engine Multiplayer: A Practical Guide
How authoritative FPS games handle the fundamental tension between server authority and client responsiveness — and how to implement it correctly in Unreal Engine.
The Core Problem
Multiplayer FPS games face an impossible-sounding constraint: the server must be the authority on all game state (to prevent cheating), but hits must feel instant to the client (or the game feels broken). Players at 100ms ping would have to lead their shots by 10cm at close range. That's unacceptable.
The solution is lag compensation via server-side rewind.
How It Works
When a player fires:
- The client records the timestamp of the shot and sends it to the server with the aim direction.
- The server receives the message some milliseconds later.
- The server rewinds all relevant hitboxes to the position they were at the client's recorded timestamp.
- The server performs the hit check against the rewound hitboxes.
- If a hit is confirmed, the damage is applied — even if the target has since moved.
From the shooter's perspective, hitting feels immediate. From the target's perspective, they can be hit a brief moment after moving to cover. This is the trade-off every online FPS makes.
Implementation in Unreal Engine
Unreal provides ULagCompensationComponent in its Lyra sample project, but it's worthwhile to understand the core loop:
FFramePackage ULagCompensationComponent::GetFrameToCheck(
const ACharacter* HitCharacter, float HitTime)
{
// Walk backwards through the saved frame history
auto& History = HitCharacter->LagCompensation->FrameHistory;
FFramePackage FrameToCheck;
for (auto It = History.GetHead(); It; It = It->GetNextNode())
{
if (It->GetValue().Time <= HitTime)
{
// Interpolate between this frame and the next for accuracy
FrameToCheck = InterpBetweenFrames(
It->GetValue(),
It->GetPrevNode()->GetValue(),
HitTime
);
break;
}
}
return FrameToCheck;
}
The key implementation detail: you must maintain a ring buffer of hitbox snapshots on the server, updated every frame (or every network tick). The depth of the buffer sets your maximum compensatable latency.
What Not to Compensate
Lag compensation is only appropriate for hitscan and fast projectiles. Do not compensate:
- Melee attacks (they already feel instant at close range)
- Area-of-effect damage (would create confusing dead zones)
- Player-friendly abilities
The Honest Trade-Off
Lag compensation is a quality-of-life choice that slightly disadvantages defenders to benefit attackers. Every serious FPS makes this call differently. CS:GO compensates fully. Valorant caps compensation at 200ms. PUBG introduced a "blue zone" anti-cheat that limits it further.
Know your game's feel goals before implementing.