0
0
Unityframework~10 mins

State synchronization in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - State synchronization
Local State Change
Send Update to Network
Receive Update on Other Clients
Apply Update to Local State
State is Synchronized
Repeat on Next Change
State synchronization means when one player changes something, that change is sent to others and applied so everyone sees the same thing.
Execution Sample
Unity
void UpdatePosition(Vector3 newPos) {
    transform.position = newPos;
    CmdSendPosition(newPos);
}

[Command]
void CmdSendPosition(Vector3 pos) {
    RpcUpdatePosition(pos);
}

[ClientRpc]
void RpcUpdatePosition(Vector3 pos) {
    transform.position = pos;
}
This code updates an object's position locally, sends the new position to the server, which then tells all clients to update their positions.
Execution Table
StepActionLocal PositionNetwork Message SentNetwork Message ReceivedPosition Updated on Clients
1Player moves object locally(1,0,0)Send CmdSendPosition(1,0,0)NoNo
2Server receives CmdSendPosition(1,0,0)Send RpcUpdatePosition(1,0,0)NoNo
3Clients receive RpcUpdatePosition(1,0,0)NoRpcUpdatePosition(1,0,0)Update position to (1,0,0)
4State synchronized on all clients(1,0,0)NoNoPosition matches on all clients
5Player moves object locally again(2,0,0)Send CmdSendPosition(2,0,0)NoNo
6Server receives CmdSendPosition(2,0,0)Send RpcUpdatePosition(2,0,0)NoNo
7Clients receive RpcUpdatePosition(2,0,0)NoRpcUpdatePosition(2,0,0)Update position to (2,0,0)
8State synchronized on all clients(2,0,0)NoNoPosition matches on all clients
💡 No more position changes; all clients have synchronized positions.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7Final
Local Position(0,0,0)(1,0,0)(1,0,0)(2,0,0)(2,0,0)(2,0,0)
Network Message SentNoneCmdSendPosition(1,0,0)NoneCmdSendPosition(2,0,0)NoneNone
Network Message ReceivedNoneNoneRpcUpdatePosition(1,0,0)NoneRpcUpdatePosition(2,0,0)None
Position Updated on Clients(0,0,0)(0,0,0)(1,0,0)(1,0,0)(2,0,0)(2,0,0)
Key Moments - 3 Insights
Why does the local position update before the network message is sent?
Because the player moves the object locally first (see Step 1 in execution_table), then the change is sent to others. This makes the game feel responsive.
What happens if the network message is delayed?
Other clients keep the old position until they receive the update (see Step 3 and 7). This can cause temporary differences until synchronization.
Why do we use a Command and then a ClientRpc?
The Command sends data from client to server (Step 1 to 2), and the ClientRpc sends data from server to all clients (Step 2 to 3), ensuring all clients get the update.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the local position after Step 5?
A(1,0,0)
B(0,0,0)
C(2,0,0)
D(3,0,0)
💡 Hint
Check the 'Local Position' column at Step 5 in the execution_table.
At which step do clients update their position to (1,0,0)?
AStep 3
BStep 1
CStep 5
DStep 7
💡 Hint
Look at the 'Position Updated on Clients' column for when position changes to (1,0,0).
If the CmdSendPosition message is never sent, what happens to other clients?
AThey update position anyway
BThey keep old position and never synchronize
CThey crash
DThey send their own position back
💡 Hint
Refer to the 'Network Message Sent' and 'Position Updated on Clients' columns in the execution_table.
Concept Snapshot
State synchronization in Unity:
- Local changes update immediately for responsiveness.
- Commands send changes from client to server.
- ClientRpc broadcasts updates from server to all clients.
- Other clients apply updates to keep states in sync.
- Network delays can cause temporary differences.
Full Transcript
State synchronization means when a player changes something locally, that change is sent to the server using a Command. The server then sends the update to all clients using a ClientRpc. Each client applies the update to keep the game state the same everywhere. This process repeats whenever the state changes. The local player sees immediate changes for smooth gameplay, while others update when they receive network messages. If messages are delayed or lost, clients may temporarily have different states until synchronization happens.