3. Persistence
Physics & Persistence
This scene will show you how easy it is to set up Networking in your Unity project and sync GameObject transforms and Physics objects across the network whilst keeping them persistent. As long as the server is running you can disconnect and re-connect, your world will persist.
In this example each client will have it's own player character to move, we will click on the map to make the entity move to that location, simple click to move functionality and right click to spawn local physics based objects that all other player characters can interact with. Each client will only have control of its local entity. You can disconnect and re-connect and the persistent entities will all remain.
General Set Up
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 Entity
is the prefab that will change per Scene with different functionality. It has a standard CharacterController
and Rigidbody
as well as an Agent
script which will handle movement functionality through the Input Manager
in the Core Scene Setup
prefab.
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.
In This Scene...
The Physics Entity Spawner
component is a simple script to instantiate a Coherence Entity Physics Persistent
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 Physics Persistent
has Entity Lifetime Type
set to Persistent
. This means it will remain in the world as long as the replication server is running, even if all clients disconnect. It also has Authority Transfer Style
set to Stealing
which means the entity can be "stolen" and simulated on a client requesting authority.
The Auto Generate Persistance UUID
should be checked so that a unique identification string will be used to identify instances of this entity as unique. When the entity is spawned with this checked, it will generate its own UUID.
This is done via the Input Manager
in the Core Scene Setup
prefab. When the object is left-clicked, it sends the message "Adopt" to the GameObject on the specific Layer Mask "Physics Entities". The component called Coherence Handler
on Coherence Entity Physics Persistent
handles the Adopt call and requests the authority change via the coherenceSync component.
Coherence Handler
is a basic layer for handling Commands and Events, both sending and receiving. You can create your own or reuse and extend this for your project.
Build and Try
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.
Last updated