Skip to main content

Character Off Ledge Handling

✍ Last Updated : December 20, 2022

🚪 Prequisite Knowledge (Optional)

Describe Initial Knowledge that reader need to know before reading this article

❓ Key Question / Problem / Issue

When character is moving over the edge, the default movement is straight forward (zero vertical velocity).

✅ Expected Output/Definition of Done

When character is moving over the edge, the character should carry out the vertical momentum.

🎁 Resulting Solution

When a character, in this case, a skater is moving on a ramp, the expected behavior is he/she jumps up carrying the vertical momentum, the same should be applies when the character is moving on a downhill ledge, he/she should jump down the ledge. But Unreal’s CharacterMovement default behavior dictates that the character jumps straight forward, ignoring the momentum from previous movement.

Red is default behavior
Green is expected behavior

Red is default behavior Green is expected behavior

The problem can be fixed by using CharacterMovement’s OnWalkingOffLedge event

ACharacter::OnWalkingOffLedge

By setting CharacterMovement.Velocity to the previously stored CharacterMovement’s LastUpdatevelocity inside the CharacterMovement’s OnWalkingOffLedge event handler, the character will jump off the ledge while preserving the momentum

Untitled

But a new problem appears, CharacterMovement.SetVelocity is not synchronized between players, it only happens on server/host. Players jump properly on server, but on client, the multiplayer reconciliation system will force the client’s player movement back to default behavior.

Red is client’s player
Green is on server’s player

Red is client’s player Green is on server’s player

The first attempt to fix the new problem is using RPC to call the WalkingOffLedge function on Server (from client). But the result is not good since there’s a delay between the time player (client’s) is moving off ledge and the time the WalkingOffLedge is called, the delay is small but enough to make player miss their jump.

Untitled

The 2nd attempt to fix is to temporarily make the client authoritative when the client is jumping (WalkingOffLedge) then switch back to server authoritative when the client finished jumping. It can be achieved by using the “ServerAcceptClientAuthoritativePosition” and “IgnoreClientMovementErrorChecksAndCorrection” node

When jumping starts

When jumping starts

When jumping ends

When jumping ends

⚠️ Note that the client also must switch back to server authoritative when the player dies/falls. ⚠️