networking · git:20261010.ceeff3d · 2026-10-10 · sha256 340c3dd41fdbf7bf

networking git:20261010.ceeff3dB

Immutable. This exact content never changes and is served at /api/v1/blob/340c3dd41fdbf7bf.

No tool to install first
claude plugin marketplace add https://markdownregistry.com/mp/a/art_htiug4cpjtzwwdfj/340c3dd41fdbf7bf4e77b2cf2210ede58ef0a6f0fa7a7716802658d273d54f24.json

Then /plugin install networking@markdownregistry-htiug4cpjtzwwdfj in Claude Code. It installs this exact version, the one graded B here, pinned to the commit it was graded at, and leaves it there.

---
title: Multiplayer Networking
description: Use when the user builds multiplayer, sets up a host or server, syncs state, writes RPCs or NetworkVariables, or chooses between Netcode for GameObjects, Netcode for Entities, Mirror, and Photon Fusion. Covers authority, ownership, RPCs, prediction, and solution tradeoffs.
globs: ["**/*.cs"]
standards-version: 1.10.0
---

# Multiplayer Networking

## MCP Tools

Call `analyze_project` to see which networking package and version the project has installed before writing netcode, and `lookup_api` with `category` set to `networking` to check APIs. If the tools are unavailable, continue without them.

## 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. NGO 2.x (the Unity 6 version) uses the universal `[Rpc]` attribute with a `SendTo` target; the method name must end in `Rpc`:

```csharp
// Any client asks the server to apply damage; this body only runs on the server
[Rpc(SendTo.Server)]
private void TakeDamageRpc(int amount)
{
    _health.Value -= amount;

    if (_health.Value <= 0)
    {
        DieRpc();
    }
}

// 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 (NGO 2.0+, Unity 6)

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, `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.

## Sources

Version-specific statements above were checked on 2026-10-10 against:

- Netcode for GameObjects 2.0 changelog (Distributed Authority): <https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@2.0/changelog/CHANGELOG.html>