# Multiplayer Melee in UE5: Client Prediction and Lag Compensation

> 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.

Markdown version of https://rigbak.com/docs/universal-combat-framework/player/multiplayer-melee-combat, a page of the Universal Combat Framework documentation. Link that page when citing it.

> 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 Combat Framework 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 **UCF 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 Combat Framework 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 player | On their own screen | On the server |
| --- | --- | --- |
| Attacks, dodges, chains a combo | The move plays at the press | Confirms the move, or sends the player back to its own move |
| Lands a swing | The hit lands at contact | Checks the reported hit before any damage is dealt |
| Locks on, switches target | At once | Hears which enemy the player locked |
| Spends stamina, starts a cooldown | The cooldown blocks at once | Spends the stamina and runs the countdown |
| Draws a weapon from the belt | The draw animation plays at once | Swaps 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 **UCF Rewind History** and hit detection stays on **Client Authoritative**, the default. Add the component to each character you want checked this way.

## Set it up (5 minutes)

1. **Record where each target stood**

   Open each character a remote player can hit, enemies and player characters alike, click **Add** and pick **UCF 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. **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. **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. **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:

| Check | Refuses a reported hit when |
| --- | --- |
| **Melee Trace Window** | The 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 Through** | The target is dodging through the swing. |
| **Plausible Hit** | The target is a teammate, the attacker itself, or farther than the weapon reaches plus the movement a round trip explains. |
| **Rewind Hit** | The target carries **UCF 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 **UCF Hit Validator** ([Melee Trace and Damage](https://rigbak.com/docs/universal-combat-framework/melee-trace-and-damage#the-ability-that-confirms-a-hit)).

---

## Pick who finds the hits

| Hit Detection Authority | What it gives | Rewind | The 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 **UCF Rewind History** | Contact the moment the blade touches |
| **Server Authoritative** | The 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 compensation | Contact a round trip late |

Set it on a character's **UCF Melee Trace** component, or for every character left on **Project Default** with **Default Hit Detection Authority** in **Project Settings > Plugins > Universal Combat Framework**. 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](https://rigbak.com/docs/universal-combat-framework/player/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](https://rigbak.com/docs/universal-combat-framework/player/multiplayer#before-you-test-online): 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 **UCF Character Movement** (`UCF.State.Dashing` in the demo), so a lagging server does not pull it back.

<details>
<summary>What the server refuses beyond hits (Multiplayer)</summary>

- **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.

</details>

<details>
<summary>Rewind settings (Advanced)</summary>

| Setting | Where | Does |
| --- | --- | --- |
| **Rewind History Seconds** (1) | Project Settings > Plugins > Universal Combat Framework | How far back the server keeps where each target stood. |
| **Rewind Memory Budget MB** (8) | Same | The most memory the history may take. |
| **Distance Tolerance** (30 cm) | **Rewind Hit**, in the confirmed hit ability's **Hit Validators** | How far from the target's recorded shapes an impact point may lie and still count. |
| **Max Rewind Seconds** (0.5) | Same | The 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.

</details>

<details>
<summary>Rewind for other hit sources (Advanced)</summary>

The **UCF 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 `UUCFRewindSubsystem`.

</details>

---

## 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 **UCF Rewind History**. 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 **UCF 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 **UCF Character Movement**, which carries the dash **Grace Tag** and smooths corrections under lag, takes C++ to set on it ([Character Setup](https://rigbak.com/docs/universal-combat-framework/player/character-setup)).

**Is it built on the Gameplay Ability System?** Yes. [Melee Combat on GAS](https://rigbak.com/docs/universal-combat-framework/player/gas-melee-combat) lists what the plugin does with each part of it.

---

## Related

| Page | Why |
| --- | --- |
| [Multiplayer](https://rigbak.com/docs/universal-combat-framework/player/multiplayer) | What runs where, and the checklist before testing online |
| [Melee Trace](https://rigbak.com/docs/universal-combat-framework/melee-trace-and-damage) | Hit volumes, swing windows and which machine finds a hit |
| [Action Graph](https://rigbak.com/docs/universal-combat-framework/player/action-graph) | Moves, combos and how each runs online |
| [Combo System](https://rigbak.com/docs/universal-combat-framework/player/combo-system) | Chains, input buffering and branches |

---

Previous page: [Parry, Block, Guard Break and Executions in UE5](https://rigbak.com/docs/universal-combat-framework/player/parry-block-executions). Next page: [Melee Combat System on the Gameplay Ability System (GAS)](https://rigbak.com/docs/universal-combat-framework/player/gas-melee-combat)
