Rex G
Rex G5d ago

Real-Time Object view

I need everyone to see a game object over each of the players head in the same instance. Do I use real-time view or transform? Or is there another script I need to attach to the object?
12 Replies
kapa.ai
kapa.ai5d ago
Hi @Rex G. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To have a game object (such as a label, icon, or indicator) appear above each player's head and be visible to all players in real-time, you should use a combination of RealtimeView and RealtimeComponent (such as RealtimeTransform) on that object. Here’s how you can approach this: 1. RealtimeView:
Attach a RealtimeView to the object you want to synchronize. This is essential for identifying and synchronizing the object and its components across all clients. The RealtimeView ensures that the object and its state are tracked in the Normcore datastore and kept in sync for everyone in the room.
RealtimeView documentation 2. RealtimeTransform:
Attach a RealtimeTransform to the object if you want its position (e.g., above the player’s head) to be synchronized across all clients. This is especially important if the object needs to move with the player or be repositioned in real-time. The RealtimeTransform will ensure that the object's transform (position, rotation, scale) is updated for all players, but only the owner can move it unless ownership is transferred.
RealtimeTransform documentation 3. Parenting:
While you might be tempted to parent the object to the player’s head, be aware that Normcore does not synchronize parent-child relationships between objects. Instead, you should update the position of the object (e.g., via script) to follow the player’s head transform, and then synchronize that position using RealtimeTransform.
RealtimeView common questions Summary:
- Use RealtimeView to synchronize the object. - Use RealtimeTransform to synchronize its position above the player’s head. - Update the object’s position in your script to follow the player’s head, and let RealtimeTransform handle the network sync. If you want to synchronize additional data (like a player name or status), you can create a custom RealtimeComponent and model for that data. If you need a concrete example or code, let me know! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5d ago
<@918869683929768036> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
maxweisel
maxweisel4d ago
Attach a child of the head game object. you shouldn't need an extra view or transform for this Or make it a sibling of the head and sync the position locally to be above it
Rex G
Rex GOP3d ago
This is how you set it up? I need for everyone to see when this gameobject is active and not active. This will be on all of the player in the instances head. I need them to see when it becomes active and inactive
No description
Mechabit
Mechabit3d ago
sync a bool on the avatar to make the headphones appear @kapa.ai write some code to sync the active state of a pair of headphones on my avatar
kapa.ai
kapa.ai3d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To synchronize the active state (e.g., on/off or visible/invisible) of a pair of headphones on your avatar across all clients, you should create a custom RealtimeComponent and a corresponding RealtimeModel with a boolean property representing the active state. This approach is directly supported by Normcore’s custom data synchronization system. Below is an example based on the official documentation for synchronizing custom data. This code will let you toggle the headphones’ active state and have that change reflected for all users in the room. 1. Create the RealtimeModel for the headphones’ active state:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RealtimeModel]
public partial class HeadphonesModel {
[RealtimeProperty(1, true, true)]
private bool _isActive;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RealtimeModel]
public partial class HeadphonesModel {
[RealtimeProperty(1, true, true)]
private bool _isActive;
}
The [RealtimeModel] and [RealtimeProperty] attributes are required for Normcore to generate the networking code for your model. The boolean _isActive will be synchronized across all clients.
Creating a realtime model 2. Create the RealtimeComponent to control the headphones:
using UnityEngine;
using Normal.Realtime;

public class HeadphonesSync : RealtimeComponent<HeadphonesModel> {
[SerializeField] private GameObject headphonesObject;

protected override void OnRealtimeModelReplaced(HeadphonesModel previousModel, HeadphonesModel currentModel) {
if (previousModel != null) {
previousModel.isActiveDidChange -= OnIsActiveChanged;
}

if (currentModel != null) {
if (currentModel.isFreshModel)
currentModel.isActive = headphonesObject.activeSelf;

UpdateHeadphonesActiveState();
currentModel.isActiveDidChange += OnIsActiveChanged;
}
}

private void OnIsActiveChanged(HeadphonesModel model, bool isActive) {
UpdateHeadphonesActiveState();
}

private void UpdateHeadphonesActiveState() {
headphonesObject.SetActive(model.isActive);
}

public void SetHeadphonesActive(bool isActive) {
model.isActive = isActive;
}
}
using UnityEngine;
using Normal.Realtime;

public class HeadphonesSync : RealtimeComponent<HeadphonesModel> {
[SerializeField] private GameObject headphonesObject;

protected override void OnRealtimeModelReplaced(HeadphonesModel previousModel, HeadphonesModel currentModel) {
if (previousModel != null) {
previousModel.isActiveDidChange -= OnIsActiveChanged;
}

if (currentModel != null) {
if (currentModel.isFreshModel)
currentModel.isActive = headphonesObject.activeSelf;

UpdateHeadphonesActiveState();
currentModel.isActiveDidChange += OnIsActiveChanged;
}
}

private void OnIsActiveChanged(HeadphonesModel model, bool isActive) {
UpdateHeadphonesActiveState();
}

private void UpdateHeadphonesActiveState() {
headphonesObject.SetActive(model.isActive);
}

public void SetHeadphonesActive(bool isActive) {
model.isActive = isActive;
}
}
This script synchronizes the active state of the headphones GameObject. When you call SetHeadphonesActive(true/false), the change is propagated to all clients, and the headphones’ visibility is updated accordingly.
Creating a RealtimeComponent subclass How to use: - Attach the HeadphonesSync script to your avatar. - Assign the headphones GameObject in the inspector. - Call SetHeadphonesActive(true) or SetHeadphonesActive(false) to toggle the headphones for all users. This approach ensures the active state of the headphones is kept in sync for all players in the room.
If you need more details on setting up custom models and components, see the Synchronizing Custom Data guide. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3d ago
<@162940911964258305> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Rex G
Rex GOP3d ago
I need to be able to click something in the actual scene to set the active state inactive and active using this. Can I add this to the scene?
Mechabit
Mechabit3d ago
when you click that thing call this function SetHeadphonesActive(true)
Mechabit
Mechabit3d ago
Synchronizing Custom Data | Normcore Documenation
In this guide, I’m going to walk you through how to synchronize custom data in your Unity application using custom realtime components.
Rex G
Rex GOP2d ago
I am using this to call the method is this correct?
No description
No description
Rex G
Rex GOP2d ago
And will this work the same when I make the object inactive?

Did you find this page helpful?