In this lesson, you will learn how to create a responsive player experience by executing local gameplay logic immediately before the server validates the action.
The Problem: Network Latency
When a player triggers an action (like jumping or firing a weapon), a standard Server RPC must travel from the client to the server, be processed, and potentially be sent back to the client. On high-latency connections, this creates a frustrating "input lag" where the character feels sluggish because they are waiting for the server's permission before reacting visually.
The Solution: Client-Side Prediction
Client-side prediction hides this latency by running the simulation locally on the client the moment the input occurs. Instead of waiting for a round-trip confirmation, the client updates its own state instantly to match the predicted outcome.
For example, when a player presses the jump key, the client immediately applies the vertical velocity to its local character movement component. Simultaneously, it sends a Server RPC to verify that jump.
void AMyCharacter::PerformJump()
{
// 1. Predict locally (Immediate response)
LaunchCharacter(FVector(0, 0, JumpForce), false, false);
// 2. Request authoritative confirmation
Server_RequestJump();
}
Handling Discrepancies
The primary challenge with this approach is "reconciliation." If the server determines that the client’s prediction was wrong (e.g., the player was actually stunned or out of stamina), the server must force the client to snap back to the correct, authoritative state. In Unreal Engine, the CharacterMovementComponent handles this automatically for movement by comparing the client's position to the server’s calculated position and performing a "Correction" if the delta exceeds a certain threshold.
By predicting movement locally, the game feels instantaneous to the player, while the server maintains ultimate authority over the game state by serving as the final arbiter of truth.
Key Takeaway: Client-side prediction masks network latency by simulating inputs locally for immediate responsiveness while relying on the server to validate and reconcile the final game state.