Performance: Remote procedure calls
Remote procedure calls affect network latency and responsiveness in multiplayer or distributed Unity applications.
void Update() {
if (Time.time - lastSent > 0.1f) {
CmdSendPosition(transform.position);
lastSent = Time.time;
}
}
[Command]
void CmdSendPosition(Vector3 pos) {
RpcUpdatePosition(pos);
}
[ClientRpc]
void RpcUpdatePosition(Vector3 pos) {
transform.position = pos;
}void Update() {
CmdSendPosition(transform.position);
}
[Command]
void CmdSendPosition(Vector3 pos) {
RpcUpdatePosition(pos);
}
[ClientRpc]
void RpcUpdatePosition(Vector3 pos) {
transform.position = pos;
}| Pattern | Network Calls | Latency Impact | Bandwidth Usage | Verdict |
|---|---|---|---|---|
| Send RPC every frame | High (60+ per second) | High latency and lag | High bandwidth usage | [X] Bad |
| Send RPC at fixed intervals | Low (10 per second) | Lower latency | Reduced bandwidth usage | [OK] Good |
[ServerRpc] to mark methods that clients call to run on the server.[ServerRpc] public void MyMethod() { }. void [ServerRpc] MyMethod() { } places the attribute incorrectly.[ServerRpc]
public void SendMessageToClients(string msg) {
Debug.Log("Server received: " + msg);
BroadcastMessageClientRpc(msg);
}
[ClientRpc]
void BroadcastMessageClientRpc(string message) {
Debug.Log("Client received: " + message);
}
What will be printed on the client when SendMessageToClients("Hello") is called from a client?SendMessageToClients which runs on the server due to [ServerRpc]. The server logs "Server received: Hello".BroadcastMessageClientRpc on all clients, which logs "Client received: Hello" on each client.[ServerRpc]
public void UpdateScoreClientRpc(int score) {
Debug.Log("Score updated: " + score);
}NetworkConnection parameter to exclude that client from receiving the RPC.Pass the test and this pattern moves unknown