# HowToReplicatePosition.md

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

## Project Setup

First, create a new Unity project. Then, import the Diarkis Sample Package via the Unity Package Manager (for details, refer to the [Readme](/en/diarkis-client/samples/unity/field-walker.md)). Finally, add a `DiarkisNetworkManager` object to the scene and configure the connection information to the Diarkis Server. With this, the setup is complete.

## Adding Assets

To understand how to synchronize a character, we will describe the synchronization process using standard Unity assets. Create a new empty scene and follow the steps below to create a map, network logic, and a controllable character. Let's add some elements:

* Add the InputSystem prefab to handle keyboard inputs (from `Packages>Diarkis Plugin Sample>Prefabs>Common>InputSystem`).
* Replace the MainCamera object with the MainCamera from `Packages>Diarkis Plugin Sample>Prefabs>CommonMainCamera`.
* Create an empty object named `SpawnPosition` and set the desired spawn location for the player.
* Add a `DiarkisNetworkManager` object (from `Packages>Diarkis Plugin Sample>Prefabs>Common>NetworkManager`) and configure it according to the Diarkis server settings (e.g., Http Host: 192.168.100.12:7000, Auto Start Connect: true).
* Add the `RoomManager` prefab (from `Packages>Diarkis Plugin Sample>Prefabs>SceneManagers>RoomManager`).\
  If all steps have been carried out correctly, the scene structure should look as follows.

![Scene Copy](/files/nhIX9eKLf6eZqyFQlIa0)

Now, you need to configure where to spawn which player prefab in the RoomManager object.\
First, copy the prefab from `Packages>Diarkis Plugin Sample>Prefabs>MainGame>Player` and place it within your assets folder like `Assets>Prefabs`. Developers can recreate the mesh, skeleton, animations, and code (as long as the class inherits from `DiarkisSynchronizedCharacter`). The complete sample script, which includes RigidBody, Animator, Camera, and user input handler, can be found at `Packages>Diarkis Plugin Sample>Scripts>Sample>MainGame>DiarkiSampleSynchronizedCharacter`. The Diarkis Player prefab uses this script by default, but developers can create their own implementation by implementing the functions `GetLocalFrameDataPayload` and `EnqueueRemoteFrameData` that the script inherits.

![Scene Copy](/files/PJk3dIKF7NOuqTshe6Pu)

Just like the `DiarkisSynchronizedPlayer` class, there is a `DiarkisRoomObject` class that can be inherited to easily create synchronized objects in Diarkis Room modules. A basic implementation example of a room object is available at `Packages>Diarkis Plugin Sample>Scripts>Sample>MainGame>DiarkiSampleRoomObject`. This class demonstrates how to create a simple object with two shapes (sphere or cube) and a random color. Developers can override the `UpdateProperties` and `GetObjectProperties` functions to recreate similar classes and implement their custom object properties. The object properties, `Object Properties`, are of type `Dictionary<string, double>`. By calling `SetWaitForSyncPush(true)`, the properties will be sent the next time `RoomObjectSync Push` is called.

![Scene Copy](/files/OaNIyYvT471t9DSd7HRy)

All that remains is to add `Player` prefab, `RoomObject` prefab, and `SpawnPosition` to the `Instance Handler` parameter in the `RoomManager Inspector`.

![Scene Copy](/files/4Cbu6HDrSSqUnL477fGK)

Various settings can be changed in the inspector to determine how character movements are synchronized. Below are explanations for each setting:

* MaxRemoteQueueLength: The number of payloads that can be queued remotely (it's recommended to keep this number above 50).
* MaxFramePerPayload: The number of frames a single payload can contain (at maximum). If the character is moving, the next payload will be sent once this frame count is reached, or after the `Minimum Local Payload Send Frequency` delay.
* Minimum Local Payload Send Frequency: The minimum delay for sending a payload (if this delay is reached after the last payload was sent, send it without waiting to reach the maximum frame value).
* LocalFrameEnqueue Frequency: If set to 0, all frames are synchronized; otherwise, one frame is synchronized per delay based on this value.
* MaxRemoteInterpolation Duration In seconds: The maximum interpolation time allowed upon receiving player movement.
* MaxAllowedDelayInSeconds: If old remote payloads are received for some reason, they are automatically skipped.

Translated with DeepL.com (free version).

Note: For example, if the number of local frames to interpolate is 4 and the maximum number of frames per payload is 5, each payload means that 5\*5=25 frames are sent, so if the FPS is 60, it is sent approximately every 60/25=\~0.42 seconds.

![Scene Copy](/files/YzZp6Zp49cexw2bxvFql)

The scene is now ready to duplicate characters and display the remotely connected players.

![Scene Copy](/files/0paa7MzOPCxZPx83O5Q9)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.diarkis.io/en/diarkis-client/samples/unity/field-walker/howtoreplicateposition.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
