networking ยท diff
git:20260524.24cf8ba to git:20261009.5da51ff
14 added, 10 removed. Audit B to B.
What this update changes in the checks
No check changed: every fixed check gives the same answer before and after this update.
A tripped check is a label, not a verdict: read the matched text and the lines below. This week's updates that newly trip a check.
---
title: Multiplayer Networking
description: Multiplayer networking patterns with Netcode for GameObjects, Netcode for Entities, Mirror, and Photon Fusion.
globs: ["**/*.cs"]
standards-version: 1.10.0
---
# Multiplayer Networking
## Choosing a Solution
Pick based on your game's scale and genre:
| Solution | Best For | Player Count | Complexity |
|----------|----------|--------------|------------|
| Netcode for GameObjects (NGO) | Casual co-op, small groups | 2-16 | Low-Medium |
| Netcode for Entities (NFE) | Competitive action, large scale | 64+ | High |
| Photon Fusion 2 | Fast-paced PvP, high frequency | 100+ | Medium |
| Mirror | Open-source indie, full control | 32-64 | Medium |
## Netcode for GameObjects (NGO)
Unity's official networking for MonoBehaviour workflows.
### Setup
1. Add NetworkManager to the scene
2. Add NetworkObject to any networked GameObject
3. Add NetworkBehaviour scripts for networked logic
### NetworkVariables
Synchronized state across all clients:
```csharp
public class PlayerHealth : NetworkBehaviour
{
private NetworkVariable<int> _health = new(
100,
NetworkVariableReadPermission.Everyone,
NetworkVariableWritePermission.Server
);
public override void OnNetworkSpawn()
{
_health.OnValueChanged += OnHealthChanged;
}
private void OnHealthChanged(int oldValue, int newValue)
{
UpdateHealthUI(newValue);
}
}
```
### RPCs
- Remote procedure calls for event-driven communication:
+ Remote procedure calls for event-driven communication. NGO 2.x (the Unity 6 version) uses the universal `[Rpc]` attribute with a `SendTo` target; the method name must end in `Rpc`:
```csharp
- // Client requests the server to do something
- [ServerRpc]
- private void TakeDamageServerRpc(int amount)
+ // Any client asks the server to apply damage; this body only runs on the server
+ [Rpc(SendTo.Server)]
+ private void TakeDamageRpc(int amount)
{
- if (!IsServer) return;
_health.Value -= amount;
if (_health.Value <= 0)
{
- DieClientRpc();
+ DieRpc();
}
}
- // Server tells all clients something happened
- [ClientRpc]
- private void DieClientRpc()
+ // Server tells every client (and the host) that the player died
+ [Rpc(SendTo.ClientsAndHost)]
+ private void DieRpc()
{
PlayDeathAnimation();
}
```
+ The older `[ServerRpc]` / `[ClientRpc]` attributes still compile, but new code should use `[Rpc]`. Validate RPC arguments on the server, since any client can call a `SendTo.Server` RPC unless you check `RpcParams` or ownership.
+
### Ownership
- By default, the server owns all objects
- Transfer ownership for player-controlled objects: `NetworkObject.ChangeOwnership(clientId)`
- Check ownership: `IsOwner`, `IsServer`, `IsClient`, `IsLocalPlayer`
### Distributed Authority (Unity 6.4)
New mode for client-hosted games with shared authority:
- No dedicated server required
- Clients share ownership of objects
- Reduces latency for certain game types
- Configure in NetworkManager settings
## Netcode for Entities (NFE)
Built on ECS/DOTS for massive-scale competitive games.
- Client-side prediction out of the box
- Interpolation and lag compensation
- Ghost system for entity synchronization
- - Requires DOTS knowledge (IComponentData, SystemBase)
+ - Requires DOTS knowledge (IComponentData, `ISystem`, and `SystemBase` where managed data is needed; see the ECS/DOTS skill)
Best for: battle royales, MMO combat, large-scale simulations.
## Photon Fusion 2
Third-party, cloud-based networking with excellent performance:
- Tick-based simulation with rollback support
- Host mode and dedicated server mode
- Built-in matchmaking and lobby system
- Excellent documentation and community
```csharp
+ // NetworkInputData is your INetworkInput struct, filled in by the runner's OnInput callback
public class NetworkedPlayer : NetworkBehaviour
{
+ [SerializeField] private float _moveSpeed = 5f;
+
[Networked] public float Health { get; set; }
[Networked] public Vector3 Position { get; set; }
public override void FixedUpdateNetwork()
{
if (GetInput(out NetworkInputData input))
{
Position += input.Direction * Runner.DeltaTime * _moveSpeed;
}
}
}
```
## Mirror
Open-source, community-maintained networking:
- Drop-in replacement for the old UNet API
- SyncVar for state synchronization
- Command/ClientRpc for communication
- Many community transports (WebSocket, Steam, KCP)
```csharp
public class PlayerCombat : NetworkBehaviour
{
[SyncVar(hook = nameof(OnHealthChanged))]
public int health = 100;
[Command]
public void CmdTakeDamage(int amount)
{
health -= amount;
}
private void OnHealthChanged(int oldValue, int newValue)
{
UpdateHealthBar(newValue);
}
}
```
## General Networking Principles
- **Server Authority**: Server validates all game state changes. Clients send input, server processes it. Prevents cheating.
- **Client Authority**: Clients control their own state directly. Simpler but vulnerable to cheating. Only use for non-competitive games.
- **Lag Compensation**: Server rewinds game state to the time the client fired, checks hit detection, then applies the result.
- **Interpolation**: Smooth remote player movement between network updates.
- **Prediction**: Client predicts the result of its own input immediately. Server corrects if the prediction was wrong.
- **Always validate on the server**: Never trust client data in competitive games.