L’interface est localisée ; les présentations et documents des Skills sont traduits automatiquement par IA dans votre langue. Le code, les commandes, chemins et termes techniques restent en version originale, et le document de l’auteur fait foi.
Explorer les Skills
Développement · SKILL.md

unity-netcode

unity-netcode

Unity Netcode for GameObjects (NGO) — NetworkBehaviour, NetworkVariable, RPCs (Server/Client/Owner), client-side prediction, lag compensation, Multiplay/Relay/Lobby integration. USE WHEN: building multiplayer (co-op, competitive, MMO-lite), network authority decisions, anti-cheat, matchmaking + relay. DO NOT USE FOR: P2P state-sync without NGO (Mirror, Ph…

Independent GitHub snapshot · Automated classification · Original documentation governs

Documentation de la Skill

GitHub

name: unity-netcode description: | Unity Netcode for GameObjects (NGO) — NetworkBehaviour, NetworkVariable, RPCs (Server/Client/Owner), client-side prediction, lag compensation, Multiplay/Relay/Lobby integration.

USE WHEN: building multiplayer (co-op, competitive, MMO-lite), network authority decisions, anti-cheat, matchmaking + relay.

DO NOT USE FOR: P2P state-sync without NGO (Mirror, Photon — out of scope); ECS-native networking (NetCode for Entities — adjacent stack). allowed-tools: Read, Grep, Glob, Write, Edit

Unity Netcode for GameObjects

Architecture

┌────────────┐         ┌───────────┐
│   Client   │  RPCs   │  Server   │  authoritative
│            │◀───────▶│ (or Host) │  state — broadcasts via
│ predicts   │  Vars   │           │  NetworkVariable + RPC
└────────────┘         └───────────┘

Use server-authoritative: never trust the client for damage, currency, or movement validation.

NetworkBehaviour basics

public class PlayerController : NetworkBehaviour {
    public NetworkVariable<int> Health = new(100,
        NetworkVariableReadPermission.Everyone,
        NetworkVariableWritePermission.Server);

    [Rpc(SendTo.Server)]
    public void FireRpc(Vector3 origin, Vector3 dir) {
        if (!IsServer) return;
        // server-side validation + spawn projectile
        SpawnProjectileServerSide(origin, dir, OwnerClientId);
    }

    [Rpc(SendTo.NotMe)]
    public void OnHitEffectRpc(Vector3 pos) {
        // VFX on every client except sender
    }
}

RPC targets (Unity 6 attribute syntax)

SendToUse
ServerClient → server requests
OwnerServer → owning client only
NotOwnerServer → all except owner (for own client prediction)
EveryoneServer → all clients (state broadcast)
NotMeCommon for VFX/SFX echo
ClientsAndHostMostly for host-aware logic

Client-side prediction

  1. Client samples input → applies move locally + sends to server.
  2. Server validates → broadcasts authoritative state.
  3. Client reconciles: if server state diverges from prediction, replay inputs from divergence frame.

NGO doesn't ship a prediction system out-of-the-box — you build one with NetworkTransform.AuthoritativeMode = Owner + custom interpolation, or move to NetCode for Entities for first-class prediction.

Lag compensation (hit-scan)

When server processes a fire RPC:

  1. Read client RTT → rewind world state to now - rtt/2.
  2. Raycast against rewound colliders.
  3. Apply damage as if the shot landed at that historical state.

Implement via a ring buffer of past Transform/Collider snapshots (e.g. 1s @ 20Hz).

Unity Gaming Services integration

  • Relay — NAT punchthrough; host gets a Join Code that clients use.
  • Lobby — pre-game rooms with metadata; passes Relay Join Code to clients.
  • Matchmaker — skill/region-based queue; spins up Multiplay servers.
  • Multiplay — dedicated server hosting; build Linux server build, deploy via UGS dashboard.

Anti-patterns

Anti-patternFix
Client-authoritative damageAlways validate on server
NetworkVariable<T> for every transform fieldUse NetworkTransform or batched update RPC
RPCs for high-frequency stateUse NetworkVariable (delta-compressed) or unreliable channel for visuals
Singleton NetworkManager accessed before spawnSubscribe NetworkManager.OnServerStarted/OnClientConnectedCallback
Mixing host-only logic with IsServerUse IsHost for host-specific code paths
Not pooling spawned NetworkObjectsNGO supports prespawned pools — use them

Production checklist

  • All damage / economy / inventory mutations server-side only
  • Lag compensation for hit-scan weapons
  • NetworkObject pools for projectiles / spawned entities
  • Relay + Lobby tested on real network (cellular, restrictive NAT)
  • Server build CI pipeline (Linux IL2CPP) → Multiplay deploy
  • Client/server version negotiation on connect — reject mismatches
  • Anti-cheat: server validates inputs, throttles RPC rate
Skills enregistrées