HowToReplicatePosition.md
This document explains how to synchronize the position of a GameObject using the Diarkis plugin.
Contents
1. Project Setup
First, create a new Unity project.
Next, import the DiarkisUnityPlugin.unitypackage
(for details, please refer to HowToUsePlugin.md).
Finally, set the connection information for Diarkis in the DiarkisNetworkManager
prefab and place it in the scene.
The setup is now complete.
2. Adding Assets
Here, we will explain the steps for synchronizing a character using Unity's standard assets. For example, this description assumes the use of "Third Person Character Controller" from the Unity Asset Store. Please replace it with the asset you are using as appropriate.
Download and Import Third Person Character Controller
from the Package Manager.
Click the Import button in Import Unity Package to import.
Create a Sample folder under Assets > Diarkis
for sample work and create the following three folders.
Use the background data that comes with the Third Person Character Controller
. Copy the file from Assets > StarterAssets > ThirdPersonController > Scenes > Playground
to Assets > Diarkis > Sample > Scenes
, and rename it to DiarkisSampleScene
.
2. Automatic Room Creation
To synchronize positions, you need to be in some sort of room (Room
).
To simplify the explanation, we will use Room.JoinRandom
to automatically create a room when the scene is played.
We have prepared sample code in the HowToReplicatePostion_SampleCode
folder for this document's explanation. You can overwrite copy the included Scripts
folder to Assets > Diarkis > Sample > Scripts
for use.
Right-click to open the context menu, create an empty GameObject with
Create Empty
, and rename it toSceneManager
.Drag and drop
Diarkis > Sample > Scripts > Game > DiarkisSampleGameManager.cs
to attach it.
Similarly, create an object called
CoreEvent
.Drag and drop
Diarkis > Runtime > Extension > MonoBehaviour > Callbacks > DiarkisCoreEventCallback.cs
to attach it.Specify
SceneManager
'sDiarkisSampleGameManager.OnNetworkConnect
inOn Network Connect
.
Similarly, create an object called
RoomEvent
.Drag and drop
Diarkis > Runtime > Extension > MonoBehaviour > Replication > Room > DiarkisRoomRepManager.cs
to attach it.Specify the various callbacks (OnRoomCreate, OnRoomJoin, OnRoomLeave, OnRoomMemberJoin, OnRoomMemberLeave).
Select
SceneManager
.Attach
RoomEvent
toRoom Rep Manager
.
DiarkisRoomRepManager
is a class that inherits from DiarkisRoomEventCallback
. It not only handles Room
events as-is but also adds various convenient functions for replication.
3. Creating Character Prefabs
Next, create the character that the player will control.
Copy
PlayerArmature
fromAssets > StarterAssets > ThirdPersonController > Prefabs
toAssets > Diarkis > Sample > Prefabs
, and rename it toDiarkisPlayerArmature
.
For characters you want to synchronize positions with, you need to have them inherit from DiarkisGameObject
and correctly implement the following three functions.
HasPositionSyncData
Whether there is position sync data to send for the local character
GetPositionSyncData
Acquire position sync data for the local character
SetPositionSyncData
Set position sync data for the remote character
4. Spawn Settings
Specify where the character will be generated. Create
PlayerSpawnPoint
and specify thePosition
where the character will spawn.
Attach
DiarkisPlayerArmature
toRoomEvent
'sPlayerPrefab
.Attach
PlayerSpawnPoint
toRoomEvent
'sAutoSpawnTransform
.
Remove
PlayerArmature
fromDiarkisSampleScene
.
By playing it, the position of remote characters from other clients will be synchronized.
Last updated