I’m working my way through the networking tutorials, and as a method of understanding the networking system I’m attempting to build (from scratch) a very simple MP game that allows multiple players to move around the world.
My problem is that my code doesn’t send move information for non-local players (they move on the owner’s client, but not on any others). Here’s psuedo-code of what I’m doing:
public class PlayerController : NetworkBehaviour
{
[SyncVar]
Vector3 syncPos;
void Start()
{
if (isLocalPlayer)
// Get the rigidbody etc. }
void Update() {
if (isLocalPlayer)
// Get the input, make a vector, etc. }
void FixedUpdate() {
if (isLocalPlayer)
{
rb.AddForce( // moveVector from Update() );
syncPos = transform.position;
}
else
transform.position = syncPos;
}
}
All the examples I see use [Commands] to sync the position, my question is why doesn’t the code above work?
My reasoning is as follows (all the important stuff is in FixedUpdate() ):
- If you are a local player (e.g. you own this player object) set syncPos to your position after you apply your movement force. This client’s syncPos variable should then be automatically updated on the server for this client.
- If you are not a local player (e.g. you do not own this player), set your position to syncPos (the location the server thinks you should be).
So theoretically, as each client moves they update their syncPos, and hence the server’s idea of where they are. But no matter what I do, this doesn’t work. Should it? Or am I missing some core concept.