- Home /
Question by
NFMynster · Aug 29, 2016 at 12:00 AM ·
networkingplayermovingsynchronizationserverside
UNET moving the players via the server.
Hello! I was wondering how you exactly move the player server wise. The thing is that I have a singleton round manager, that has a method to restart the round, which basically just sets all the players transform.position to their spawn position. However, I'm struggling with syncing it properly. Should I use an ClientRPC or what? Seems like a command does nothing.
Comment
Best Answer
Answer by DiegoSLTS · Aug 29, 2016 at 01:22 AM
You can do that with an RPC call, yes. The basic idea would be:
public class Player : NetworkBehaviour {
[Server]
public void MoveTo(Vector3 newPosition) { //call this on the server
transform.position = newPosition; //so the player moves also in the server
RpcMoveTo(position);
}
[ClientRPC]
void RpcMoveTo(Vector3 newPosition) {
transform.position = newPosition; //this will run in all clients
}
}
Or with a SyncVar:
public class Player : NetworkBehaviour {
[SyncVar(hook = "MoveTo")]
public Vector3 syncPosition; //set this in the server, and set the transform.position manually too, since the hook only runs on clients
void MoveTo(Vector3 newPosition) {
transform.position = newPosition; //this will run in all clients
}
}