HowToReplicatePosition.md

This document explains how to synchronize the position of GameObjects using the Diarkis plugin.

Table of Contents

1. Project Setup

First, create a new Unity project.

Next, import DiarkisUnityPlugin.unitypackage (for details, refer to HowToUsePlugin.md).

Finally, set the connection information for Diarkis on the DiarkisNetworkManager prefab and place it in the scene.

This completes the setup.

2. Adding Assets

Here, we will explain the steps to synchronize a character using Unity's standard assets. For example, we will use "Third Person Character Controller" available in Unity's Asset Store. You can replace this with any asset you are using as needed.

Download and import Third Person Character Controller from the Package Manager.

Click the Import button in the Unity Package Import window.

For sample work, create a Sample folder under Assets > Diarkis and create the following three folders:

Use the background data associated with 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 join some room (Room).

To simplify things, we will automatically create a room using Room.JoinRandom when the scene is played.

Sample code for this document is available in the HowToReplicatePostion_SampleCode folder. You can overwrite and use the provided Scripts folder in Assets > Diarkis > Sample > Scripts.

  1. Create an empty GameObject by right-clicking and selecting Create Empty, then rename it to SceneManager.

  2. Drag and drop DiarakiSampleGameManager.cs from Diarkis > Sample > Scripts > Game to attach it.

  1. Similarly, create another object named CoreEvent.

  2. Drag and drop DiarkisCoreEventCallback.cs from Diarkis > Runtime > Extension > MonoBehaviour > Callbacks to attach it.

  3. Specify DiarakiSampleGameManager.OnNetworkConnect of SceneManager to On Network Connect.

  1. Similarly, create another object named RoomEvent.

  2. Drag and drop DiarkisRoomRepManager.cs from Diarkis > Runtime > Extension > MonoBehaviour > Replication > Room to attach it.

  3. Specify the various callbacks (OnRoomCreate, OnRoomJoin, OnRoomLeave, OnRoomMemberJoin, OnRoomMemberLeave).

  1. Select SceneManager.

  2. Attach RoomEvent to Room Rep Manager.

DiarkisRoomRepManager is a class that inherits from DiarkisRoomEventCallback and not only handles Room events but also comes with various useful replication features.

3. Creating the Character Prefab

We will now create the character that the player will control.

  1. Copy PlayerArmature from Assets > StarterAssets > ThirdPersonController > Prefabs to Assets > Diarkis > Sample > Prefabs, and rename it to DiarkisPlayerArmature.

To synchronize a character's position, inherit DiarkisGameObject and correctly implement the following three functions:

Function NameRole

HasPositionSyncData

Checks if there is position synchronization data to send for the local character

GetPositionSyncData

Retrieves the position synchronization data for the local character

SetPositionSyncData

Sets the position synchronization data for the remote character

4. Setting the Spawn

  1. Specify the location where the character will be spawned. Create PlayerSpawnPoint and set the spawn Position.

  1. Attach DiarkisPlayerArmature to PlayerPrefab of RoomEvent.

  2. Attach PlayerSpawnPoint to AutoSpawnTransform of RoomEvent.

  1. Delete PlayerArmature from the DiarkisSampleScene.

  1. By pressing Play, you can now synchronize the positions of remote characters from other clients.

Last updated