Multiplayer Melee in UE5: Client Prediction and Lag Compensation

Documentation Unreal Engine Player Combat Multiplayer Tutorial

Online melee for Unreal Engine 5 co-op that does not feel laggy: attacks predicted at the press and rolled back if the server refuses, plus opt-in server-side hit rewind.


Melee that feels laggy online, in co-op or PvP, comes down to two things: a swing has to answer the button at once, and a hit has to count where the attacker saw it. Universal Player Combat predicts each move on the player's machine, and the server confirms it or rolls it back. Under the default hit detection, the player's machine finds its own hits and the server checks each one it reports. On a character that carries the UPC Rewind History component, which you add per character, the server also checks a reported hit against where that character stood when the attacker swung: server-side rewind, a form of lag compensation.
It runs on Unreal Engine 5.6, 5.7 and 5.8, on a listen server and on a dedicated server, set up in Blueprint and data assets.

The problem

The server owns the truth, and each message to it takes a round trip: a packet to the server and back. If an attack waits for the server, every swing feels late. If the player's machine plays it at once, the server has to agree, and say what happens when it does not.
Hits are harder. Each player sees the other characters a little in the past, so a swing that connects on the attacker's screen can miss the target at the place the server holds for it. Lag compensation answers that: the server keeps a short history of where each character stood and judges a reported hit at the moment the attacker saw. Building that by hand means predicted abilities, a rollback when the server refuses one, a position history per character and a check on each reported hit. Universal Player Combat ships each of those; the position history is the part you switch on per character.

What answers at once, and what the server decides

The playerOn their own screenOn the server
Attacks, dodges, chains a comboThe move plays at the pressConfirms the move, or sends the player back to its own move
Lands a swingThe hit lands at contactChecks the reported hit before any damage is dealt
Locks on, switches targetAt onceHears which enemy the player locked
Spends stamina, starts a cooldownThe cooldown blocks at onceSpends the stamina and runs the countdown
Draws a weapon from the beltThe draw animation plays at onceSwaps the item about a round trip later
Damage, health and each defense, a parry included, resolve on the server.

Turn on hit rewind and test it under lag

Rewind is on for a character once it carries UPC Rewind History and hit detection stays on Client Authoritative, the default. No demo character carries the component, so add it first.

Set it up

5 minutes
  1. 1

    Record where each target stood

    Open each character a remote player can hit, enemies and player characters alike, click Add and pick UPC Rewind History. The server keeps about a second of where each one stood. A target without it skips the rewind check and keeps the other checks.
  2. 2

    Play as two clients

    In the Play dropdown, set Net Mode to Play As Client and Number of Players to 2: a dedicated server with two players.
  3. 3

    Add lag

    In Editor Preferences > Level Editor > Play > Multiplayer Options, tick Enable Network Emulation, set Emulation Target to Everyone, and pick the Network Emulation Profile Average, or Bad for a poor connection.
  4. 4

    Swing at a moving target

    Chain a combo at an enemy in one of the demo's arenas. The swings answer each click at once, and the target's health drops a round trip later, once the server has checked each hit against where the target stood on your screen.
To lag one machine only, use its console: NetEmulation.PktLag 80, NetEmulation.PktLagVariance 20, NetEmulation.PktLoss 5, and NetEmulation.Off to stop.

How the server checks a reported hit

Under Client Authoritative, the attacking player's machine finds the hit at contact, so the swing connects on screen the moment it touches. The server runs four checks on each hit it reports before any damage, and a hit failing one deals nothing:
CheckRefuses a reported hit when
Melee Trace WindowThe attacker's montage is not playing the swing window the hit names, so a report cannot name a heavier attack than the one playing.
Pass ThroughThe target is dodging through the swing.
Plausible HitThe target is a teammate, the attacker itself, or farther than the weapon reaches plus the movement a round trip explains.
Rewind HitThe target carries UPC Rewind History and the impact point lay farther than Distance Tolerance from it where the attacker saw it, about a round trip earlier. On a target without the component, this check passes the hit.
These checks bound a reported hit; they do not prove the swing touched the target on the attacker's screen. A modified client can still report a hit that passes them. A competitive game takes hit detection off the client with Server Authoritative, below.
A hit that passes still lands only as often, and on as many targets, as its swing allows. The checks live on the character's confirmed hit ability as its Hit Validators, and a check of your own is a Blueprint child of UPC Hit Validator (Melee Trace).

Pick who finds the hits

Hit Detection AuthorityWhat it givesRewindThe attacker sees
Client Authoritative (default)The player's machine finds its own hits and the server checks each report. Suits co-op and PvE.Applies, on targets that carry UPC Rewind HistoryContact the moment the blade touches
Server AuthoritativeThe server finds every hit itself, against where targets stand on the server, and refuses reported hits. No client decides a hit, which suits competitive PvP.Does not apply: no hit is reported, and the server judges each hit at its own present, with no lag compensationContact a round trip late
Set it on a character's UPC Melee Trace component, or for every character left on Project Default with Default Hit Detection Authority in Project Settings > Plugins > Universal Player Combat. The server finds the hits of characters no player controls, such as enemies, under either setting.

Moves: predicted, then confirmed

Each move in the Action Graph runs as a gameplay ability predicted on the player's machine: it starts at the press, and the server confirms it once the move is reachable from the one before, its gates are open and its costs affordable. A move the server refuses rolls the character back to the server's move, and the waiting press clears.
Before testing online, check the short list on Multiplayer: a team on the pawn rather than the controller, combat style fragments on Every Machine, and a fast dash that holds the Grace Tag of UPC Character Movement (UPC.State.Dashing in the demo), so a lagging server does not pull it back.
MultiplayerWhat the server refuses beyond hits
  • Moves: which may follow which, and how soon. For a player on another machine, the server refuses a chain that came sooner than the move's earliest action window by more than Dwell Grace Base Seconds (0.1) plus the round trip.
  • Costs and cooldowns, equipment changes, interaction reach, and how far a dash carries the player.
  • A press made right after a draw, before the server has sent that weapon's moves, goes to the server and plays about 1.5 round trips later; Max Server Inputs Per Second (20) caps these per player.
AdvancedRewind settings
SettingWhereDoes
Rewind History Seconds (1)Project Settings > Plugins > Universal Player CombatHow far back the server keeps where each target stood.
Rewind Memory Budget MB (8)SameThe most memory the history may take.
Distance Tolerance (30 cm)Rewind Hit, in the confirmed hit ability's Hit ValidatorsHow far from the target's recorded shapes an impact point may lie and still count.
Max Rewind Seconds (0.5)SameThe furthest back a target is set. A player whose round trip is longer is checked at that moment instead, so a very laggy player can lose hits.
Under packet loss, a report held up by a lost packet is checked about a round trip late, so a hit on a moving target can be refused.
AdvancedRewind for other hit sources
The UPC Rewind subsystem answers any hit source against the recorded targets, a projectile or a hitscan weapon included: Rewind Sweep sweeps a line, a sphere or a capsule against the recorded targets as they stood a given number of seconds ago, and Get View Delay says how long ago a player saw a target. Register Actor For Rewind and Register Component For Rewind record an actor without adding the component. In C++ it is UUPCRewindSubsystem.

Common questions

Does it use rollback? For moves, yes: a move the server refuses sends the character back to the server's move. A hit the server refuses deals no damage.
Does it work on a dedicated server? Yes, and on a listen server and in single player, with the same setup.
Is rewind on by default? The check is; the recording is not. Rewind Hit sits in the confirmed hit ability's checks from the start, but it judges only targets that carry UPC Rewind History, and no demo character carries it. Add the component to each character a remote player can hit, and keep Hit Detection Authority on Client Authoritative.
Does it stop cheated hits? It bounds them. Under Client Authoritative the server checks each reported hit against its swing window, its reach, a dodge and, on recorded targets, where the target stood, and a modified client can still report a hit inside those bounds. Under Server Authoritative no client reports a hit at all.
Do enemies need prediction? No. The server runs characters no player controls and finds their hits itself; other machines play what it sends.
Does it need C++? Not on a character built on UPC Character Base: each step on this page is a component, a project setting or an editor preference. A character that keeps its own parent class moves with the engine's Character Movement, and UPC Character Movement, which carries the dash Grace Tag and smooths corrections under lag, takes C++ to set on it (Character Setup).
Is it built on the Gameplay Ability System? Yes. Melee Combat on GAS lists what the plugin does with each part of it.

PageWhy
MultiplayerWhat runs where, and the checklist before testing online
Melee TraceHit volumes, swing windows and which machine finds a hit
Action GraphMoves, combos and how each runs online
Combo SystemChains, input buffering and branches