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 a right click will spawn local physics based objects that all other players will see. These physics objects will interact with each other and the physics for every object will be simulated locally on its authority. Clicking one of these objects will either take or relieve authority of the local client over the clicked object. You can disconnect and re-connect and the persistent entities will all remain.
The controls at the top right of the screen allow the spawning of a unique object that works very similarly to the other physics based objects. This bigger cube is set to No Duplicates
in it's Uniqueness
property which means the server will only allow one instance of this object to exist at a time. It also has it's Lifetime
setting set to Session Based
this will cause the object to be deleted when it's owner disconnects. Just like with the physics based objects, a player can claim authority over this object by clicking it. If that player would then disconnect all clients will have the unique object deleted.
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 not present in this scene.
Input Manager
in the Core Scene Setup
prefab is set up to spawn a Sample Entity Physics Persistent
where a click is performed.
Coherence Connection Events
handles overall Scene connectivity. In this scene we use it to cleanup objects the client has authority over when disconnecting.
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.
The Coherence Entity Physics
variants have an 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.
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
objects handle 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