chart-networkExample - Unity NGO

Netcode for GameObjects Integration

This guide walks you through integrating Enemy Masses with Unity's Netcode for GameObjects (NGO) for multiplayer gameplay, including RTS, co-op, and horde-style games.


Prerequisites

  • Unity 6 LTS or newer

  • Netcode for GameObjects package (com.unity.netcode.gameobjects)

  • Enemy Masses 1.4.0+

// Package Manager
com.unity.netcode.gameobjects: 2.7.0+

Game Mode Considerations

Enemy Masses supports different multiplayer game types with varying synchronization strategies:

RTS / Strategy Games

  • Players control specific factions — ownership validation required

  • Commands synced — move, attack, stop commands sent to server

  • Moderate position sync — periodic corrections (0.2-0.5s intervals)

  • Strict validation — verify ownership and reasonable distances

Co-op / Horde / Survivor Games (e.g., Vampire Survivors-style)

  • All enemies are AI-controlled — server owns all enemies

  • Player positions synced — enemies target nearest synced player

  • High position sync rate — frequent updates (0.1s) for smooth enemy movement

  • Relaxed validation — generous distance tolerances for hit detection

Key Architecture Difference

Approach
Best For
Server Role
Client Role

Command-Based

RTS

Validates & executes commands

Sends commands, receives corrections

Position-Sync

Co-op/Horde

Runs all AI, broadcasts positions

Visual-only for enemies, sends player pos

Important: Unity's NavMeshAgent is not deterministic across machines. For games needing accurate hit validation (distance checks, facing, etc.), the server should run the simulation and broadcast positions. Clients should not run independent enemy AI simulations and expect them to match.


Architecture Overview


Step 1: Create the Network Authority Implementation

Create a new script that implements INetworkAuthorityProvider using NGO:


Step 2: Implement Damage Authority

Handle damage validation and synchronization:


Step 3: Add RPCs to Network Authority

Add the RPC methods to NGONetworkAuthority:


Step 4: Implement Command Authority

Handle RTS commands (move, attack, stop):


Step 5: Add Command RPCs

Add these RPCs to NGONetworkAuthority:


Step 6: Implement Spawn Authority

Handle synchronized agent spawning:


Step 7: Add Spawn RPCs


Step 8: State Synchronization

Implement periodic state sync for large-scale battles:

Add the sync RPCs:


Step 9: Setup Anti-Cheat (Server Only)

Add anti-cheat to your server:


Step 10: Scene Setup

  1. Create a NetworkManager prefab with NGO's NetworkManager component

  2. Create a NetworkAuthority prefab:

    • Add NetworkObject component

    • Add NGONetworkAuthority component

    • Mark as Spawn With Player or instantiate on server start

  3. Ensure your Enemy Masses prefabs are in the scene:

    • EnemyMassesCrowdController

    • EnemyMassesRTSController

    • Your crowd prefabs


Network Topology Options

Host Mode (Good for Casual/Co-op)


Performance Tips

1. Batch Network Calls

2. Use Delta Compression

3. Prioritize Important Agents

4. Use Unreliable for Position Updates


Late-Joiner Synchronization

When a player joins mid-game, they need the full current state of all agents. The NGONetworkAuthority handles this automatically via OnClientConnected.

How It Works

  1. Player ConnectsOnClientConnected fires on the server

  2. Server Waits → Short delay (0.5s) ensures client is ready

  3. Full State Sent → Server sends AgentFullState for every agent (batched in groups of 50)

  4. Client Applies → Client spawns missing agents and updates existing ones

What Gets Synced

Property
Description

networkId

Unique identifier for the agent

factionIndex

Which faction the agent belongs to

position

World position (Vector3)

rotation

World rotation (Quaternion)

health

Current health value

isDead

Whether agent is dead

ownerPlayerId

Which player owns this agent

Custom Late-Joiner Logic

Extend the late-joiner sync to include your custom game state:


Unity's NavMeshAgent is not deterministic across different machines. Even with identical inputs, agents will drift apart over time due to:

  • Floating-point differences between CPUs

  • Frame rate variations

  • Path recalculation timing

  • Local avoidance interactions

The Solution: Server Authority

This guide uses server-authoritative positions:

  1. Server runs all NavMeshAgents and owns the "truth"

  2. Clients receive position updates periodically via NGOStateSync.SyncAllAgents()

  3. Correction threshold prevents jitter from small differences

Tuning Position Sync

Scenario
Sync Interval
Correction Threshold

RTS (low precision)

0.2s

2.0f

Action game

0.1s

0.5f

High precision

0.05s

0.25f

Bandwidth Note: Lower intervals = more network traffic but more accurate positions.

Client-Side Interpolation

For smoother visuals, implement interpolation instead of instant warping:


Troubleshooting

Agents Teleporting

  • Increase sync frequency

  • Add interpolation on client

  • Check network latency

Commands Not Executing

  • Verify ownership validation

  • Check command rate limits

  • Ensure network IDs match

Desync Issues

  • Enable state reconciliation logging

  • Compare server/client states

  • Check for missing RPCs

High Bandwidth Usage

  • Reduce sync frequency

  • Enable delta compression

  • Batch smaller packets


Debug Tip: Use Tools > Enemy Masses > Network > Anti-Cheat Monitor during development to monitor cheat detections in real-time. This is an Editor-only tool; for in-game admin panels, query AntiCheatComponent.GetStatistics() directly.

Last updated