juanje89
juanje893w ago

Creating a common element that moves

Hi, how could I create a "common" element for every client that gets connected? For example, I want a gameobject that moves from position A to position B (and so forth) and, every client that connects, sees the gameobject moving in sync.
3 Replies
kapa.ai
kapa.ai3w ago
Hi @juanje89. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To create a common element that moves synchronously for all clients in NormalVR, you can use a combination of RealtimeView, RealtimeTransform, and a custom RealtimeComponent. Here's how you can achieve this: 1. Create a GameObject in your scene that will represent your moving element. 2. Add a RealtimeView component to this GameObject. This is essential for identifying the object across all clients RealtimeView documentation (https://normcore.io/documentation/realtime/realtimeview). 3. Add a RealtimeTransform component to the GameObject. This will handle the synchronization of the object's position, rotation, and scale across all clients RealtimeTransform documentation (https://normcore.io/documentation/realtime/realtimetransform). 4. Create a custom script (e.g., MovingElement.cs) that inherits from RealtimeComponent<MovingElementModel>. This script will handle the movement logic and synchronization of the movement state. 5. Create a corresponding MovingElementModel class to store the movement state. Here's a basic example of how your custom script might look:
using Normal.Realtime;
using UnityEngine;

public class MovingElement : RealtimeComponent<MovingElementModel>
{
public Vector3 positionA;
public Vector3 positionB;
public float speed = 1f;

private void Update()
{
if (model.isFreshModel)
{
model.progress = 0f;
}

float newProgress = model.progress + Time.deltaTime * speed;
if (newProgress > 1f) newProgress -= 1f;
model.progress = newProgress;

transform.position = Vector3.Lerp(positionA, positionB, model.progress);
}
}

[RealtimeModel]
public partial class MovingElementModel
{
[RealtimeProperty(1, true)]
public float progress;
}
using Normal.Realtime;
using UnityEngine;

public class MovingElement : RealtimeComponent<MovingElementModel>
{
public Vector3 positionA;
public Vector3 positionB;
public float speed = 1f;

private void Update()
{
if (model.isFreshModel)
{
model.progress = 0f;
}

float newProgress = model.progress + Time.deltaTime * speed;
if (newProgress > 1f) newProgress -= 1f;
model.progress = newProgress;

transform.position = Vector3.Lerp(positionA, positionB, model.progress);
}
}

[RealtimeModel]
public partial class MovingElementModel
{
[RealtimeProperty(1, true)]
public float progress;
}
6. Add this MovingElement script to your GameObject in the scene. 7. Make sure you have a Realtime component in your scene and it's configured to connect to a room. With this setup, when clients connect to the room, they will see the GameObject moving between position A and B synchronously. The RealtimeTransform component ensures that the position is synchronized across all clients, while the custom MovingElement script handles the movement logic. Remember to compile the MovingElementModel after creating it, as mentioned in the Synchronizing Custom Data documentation (https://normcore.io/documentation/realtime/synchronizing-custom-data). This approach leverages NormalVR's datastore synchronization, ensuring that late-joining clients will also see the object in the correct position Client Architecture documentation (https://normcore.io/documentation/architecture/client). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@759006240646103041> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
juanje89
juanje89OP3w ago
should this "moving element" be a prefab to be instantiated with Realtime.Instantiate ?

Did you find this page helpful?