- Home /
Synchronizing game object across clients
Is it possible to have a pre-existing gameobject, that is controlled via script, and synchronize its position and rotation for all clients. If yes, then how can I do it?
I'd like to have an object that already exists in the scene (I can't spawn it, it has to be an already existing in the scene game object), control its position and rotation via script on the server and have all the clients see the change in its position and rotation. How can I achieve that?
Answer by VerneVision7891 · Jan 23, 2019 at 11:06 AM
Ok, so I approached the problem differently. Instead of using Network Transform I wrote my own script. Firstly, I wrote a short script to disable on all clients the component moving the object. Then, I wrote the script to send position and rotation from server to clients:
public class NetworkShareTransformData : NetworkBehaviour
{
[SyncVar] public Vector3 currentPosition = Vector3.zero;
[SyncVar] public Quaternion currentRotation = Quaternion.identity;
[ClientRpc]
private void RpcSyncPositionWithClients(Vector3 positionToSync)
{
currentPosition = positionToSync;
}
[ClientRpc]
private void RpcSyncRotationWithClients(Quaternion rotationToSync)
{
currentRotation = rotationToSync;
}
private void Update ()
{
if (isServer)
{
RpcSyncPositionWithClients(this.transform.position);
RpcSyncRotationWithClients(this.transform.rotation);
}
}
private void LateUpdate()
{
if (!isServer)
{
this.transform.position = currentPosition;
this.transform.rotation = currentRotation;
}
}
}
Answer by tormentoarmagedoom · Jan 22, 2019 at 04:02 PM
Good day.
I think you need to watch some basic tutorials about networking and multiplayer. You are asking one of the basics... Spend 3-4 hours with some "boring but important" tutorial before commence, it will help you a lot!
PD: The easy solution is make the server to move the object and transmit the position to clients. IT's a component called Network Transform.
PD2: Spawn an object in the network is not the same as instantiate, In this context, spawn means something like "make everybody aware that this is a object in the network".
Good Luck!
Bye!
$$anonymous$$y problem is that I can easily use the Network $$anonymous$$anager and spawn an object from a prefab with a Network Transform but when I use a game object existing in the scene ins$$anonymous$$d of a prefab I can't get it to work.
Firstly, I set up a game object with the Network $$anonymous$$anager and Network $$anonymous$$anager HUD. When I put the prefab with a Network Transform in "PlayerPrefab" field, I could easily have a game object with a movement synchronised between server and client - I move the object on the server and see the movement on both the server and the client.
Can Network $$anonymous$$anager only work with prefabs? Because when I have in my scene a game object with Network Identity and Network Transform, and I move it on the server, it's only moved on the server, but not on the client. I assume that I'm missing something but I don't know what.
Your answer
Follow this Question
Related Questions
Networking Sync SetActive Not Working 0 Answers
How fast do Syncvars synchronize? 1 Answer
How to call [Command] on Client in UNet 1 Answer
How to sync variables with UNET 3 Answers