ixikos
ixikos2mo ago

Realtime.Instantiate at specific position and rotation

What is the best way to Realtime.Instantiate something at a specific position and rotation without using a RealtimeTransform component
8 Replies
kapa.ai
kapa.ai2mo ago
Hi @ixikos. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- You can use the Realtime.Instantiate() method to instantiate a prefab at a specific position and rotation. This method instantiates a realtime prefab in the scene on all clients. The fully instantiated game object, along with all RealtimeViews and RealtimeComponents, will be initialized and ready for use immediately. Here is the method signature:
Instantiate(GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, InstantiateOptions? options))
Instantiate(GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, InstantiateOptions? options))
- prefabName: The name of the prefab to instantiate. The prefab must contain a RealtimeView on the root and it must live within a Resources folder or Unity will not be able to locate it on other clients. - position: The position where you want to instantiate the prefab. - rotation: The rotation you want to instantiate the prefab with. - options: Used to override the default ownership / lifetime settings for the object or to use a specific instance of Realtime when multiple instances are available in the scene. Please note that the prefab you are instantiating should not have a RealtimeTransform component as you mentioned not wanting to use it. If you need to synchronize the position and rotation of the instantiated object, you will need to implement that manually since RealtimeTransform is not being used. For more information, you can refer to the Realtime.Instantiate documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.Realtime). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@346532823143809046> kapa.ai is still learning and improving, please let us know how it did by reacting below
ixikos
ixikos2mo ago
This doesn't seem to actually set the position or rotation, I get this warning in the console: Realtime:Instantiate() asked to set a position & rotation on prefab that doesn't have a RealtimeTransform component. The position / rotation will not be synchronized between clients. @Normcore Assistant This doesn't seem to actually set the position or rotation, I get this warning in the console: Realtime:Instantiate() asked to set a position & rotation on prefab that doesn't have a RealtimeTransform component. The position / rotation will not be synchronized between clients.
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The warning you're seeing is because the Realtime.Instantiate() method with position and rotation parameters is designed to work with prefabs that have a RealtimeTransform component. This component is responsible for synchronizing the position and rotation of the object across all clients. If you're not using a RealtimeTransform component, the position and rotation you set in the Realtime.Instantiate() method will only apply to the local instance of the object and will not be synchronized across clients. If you need to instantiate an object at a specific position and rotation without using a RealtimeTransform component, you can do so by first instantiating the object using Realtime.Instantiate(), and then manually setting the position and rotation. However, keep in mind that these values will not be synchronized across clients. Here's an example:
// Instantiate the object
GameObject myObject = Realtime.Instantiate("My Object", ownedByClient: true);

// Set the position and rotation
myObject.transform.position = new Vector3(0, 0, 0);
myObject.transform.rotation = Quaternion.identity;
// Instantiate the object
GameObject myObject = Realtime.Instantiate("My Object", ownedByClient: true);

// Set the position and rotation
myObject.transform.position = new Vector3(0, 0, 0);
myObject.transform.rotation = Quaternion.identity;
For more information, you can refer to the Realtime.Instantiate documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.Realtime). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
maxweisel
maxweisel2mo ago
Just use RealtimeTransform. Why are you trying to reinvent the wheel here?
ixikos
ixikos2mo ago
the objects have rigidbodies on them, I was wondering why I was getting so many FixedUpdate calls from the RealtimeTransform script and then realized the RealtimeTransform does more with the rigidbody there, (they are kinematic, need the rigidbody for collision purposes) I could probably refactor the prefab so the rigidbody / collider are children, that would be another approach
Mechabit
Mechabit2mo ago
if you don't want to use RT then you'll have to use a cutom model with a vector3 for position
ixikos
ixikos2mo ago
yeah thats basically what I did in this case for now, thanks !