KevinK
Normal2y ago
4 replies
Kevin

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;


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.

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.


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:
----------------------
Was this page helpful?