Kevin
Kevin2mo ago

Setting position of object not updating on other clients

I am working on a script to allow for shooting. I am spawning bullets like this:
GameObject bulletGameObject = Realtime.Instantiate(prefabName: "Bullet", // Prefab name
ownedByClient: true, // Make sure the RealtimeView on this prefab is owned by this client
preventOwnershipTakeover: true, // Prevent other clients from calling RequestOwnership() on the root RealtimeView.
useInstance: GameObject.Find("MultiplayerManager").GetComponent<Realtime>()); // Use the instance of Realtime that fired the didConnectToRoom event.
bulletGameObject.transform.position = firePoint.position;
bulletGameObject.transform.rotation = firePoint.rotation;
GameObject bulletGameObject = Realtime.Instantiate(prefabName: "Bullet", // Prefab name
ownedByClient: true, // Make sure the RealtimeView on this prefab is owned by this client
preventOwnershipTakeover: true, // Prevent other clients from calling RequestOwnership() on the root RealtimeView.
useInstance: GameObject.Find("MultiplayerManager").GetComponent<Realtime>()); // Use the instance of Realtime that fired the didConnectToRoom event.
bulletGameObject.transform.position = firePoint.position;
bulletGameObject.transform.rotation = firePoint.rotation;
The bullets on the shooters screen spawn at the correct position, but on the other client they spawn at what looks to be 0, 0, 0. How would I do this correctly?
Solution:
---------------------- The issue you're experiencing is likely due to the fact that you're setting the position of the bullet after instantiating it. When you instantiate a Realtime prefab, it is instantiated on all clients. However, the position update you're making after instantiation is not being synchronized across all clients. To solve this, you should use the Realtime.Instantiate() method that allows you to specify the position and rotation during instantiation. This will ensure that the position and rotation are set correctly on all clients. ...
Jump to solution
3 Replies
kapa.ai
kapa.ai2mo ago
Hi @Kevin. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Solution
kapa.ai
kapa.ai2mo ago
---------------------- The issue you're experiencing is likely due to the fact that you're setting the position of the bullet after instantiating it. When you instantiate a Realtime prefab, it is instantiated on all clients. However, the position update you're making after instantiation is not being synchronized across all clients. To solve this, you should use the Realtime.Instantiate() method that allows you to specify the position and rotation during instantiation. This will ensure that the position and rotation are set correctly on all clients. Here's how you can modify your code:
GameObject bulletGameObject = Realtime.Instantiate(prefabName: "Bullet", // Prefab name
position: firePoint.position, // Position
rotation: firePoint.rotation, // Rotation
ownedByClient: true, // Make sure the RealtimeView on this prefab is owned by this client
preventOwnershipTakeover: true, // Prevent other clients from calling RequestOwnership() on the root RealtimeView.
useInstance: GameObject.Find("MultiplayerManager").GetComponent<Realtime>()); // Use the instance of Realtime that fired the didConnectToRoom event.
GameObject bulletGameObject = Realtime.Instantiate(prefabName: "Bullet", // Prefab name
position: firePoint.position, // Position
rotation: firePoint.rotation, // Rotation
ownedByClient: true, // Make sure the RealtimeView on this prefab is owned by this client
preventOwnershipTakeover: true, // Prevent other clients from calling RequestOwnership() on the root RealtimeView.
useInstance: GameObject.Find("MultiplayerManager").GetComponent<Realtime>()); // Use the instance of Realtime that fired the didConnectToRoom event.
This version of the Realtime.Instantiate() method is documented here (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
<@1151175034346414100> kapa.ai is still learning and improving, please let us know how it did by reacting below