This scene will show you how easy it is to set up Networking in your Unity project and sync non player characters that move around the world via Unity's Navmesh system.
In this example each client can spawn as many navigation agents as they wish, and these navigation agents will move intermittently to different locations on the grid. All navigation agents will be replicated across clients with a specific colors signifying if they are local or networked.
In the Hierarchy of the Scene you can see three core Prefabs:
Core Scene Setup
and Coherence Setup
are present in all scenes and described in detail on Start Tutorial page.
Coherence Connection Events
handles overall Scene connectivity. Additionally, it removes all Entities with coherenceSync
from the Scene to demo disconnection/reconnection via the Interface without refreshing the Scene.
Spawner
is a simple script to instantiate a Coherence Entity Nav Agent
prefab with a coherenceSync component that replicates the transform and position. The component also changes the material based on if it is locally simulated or synced over the network.
Coherence Entity Nav Agent
has a Nav Mesh Agent
component controlled via the Navigation Agent
script which every few seconds sets a new destination on the grid. It's not required to sync anything other than the Transform; position, rotation
parameter as the Nav Mesh Agent
settings only need to simulate locally.
You can build this Scene via the Build Settings. Run the local Replication Server through the Window -> coherence -> Settings window and see how it works. You can try running multiple clients rather than just two and see replicating for each.