Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question about very simply network movement syncing

Discussion in 'Multiplayer' started by nordee, Sep 25, 2015.

  1. nordee

    nordee

    Joined:
    Sep 25, 2015
    Posts:
    5
    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:

    Code (CSharp):
    1. public class PlayerController : NetworkBehaviour
    2. {
    3.     [SyncVar]
    4.     Vector3 syncPos;
    5.  
    6.     void Start()
    7.     {
    8.         if (isLocalPlayer)
    9.             // Get the rigidbody etc. }
    10.  
    11.     void Update() {
    12.     if (isLocalPlayer)
    13.         // Get the input, make a vector, etc.  }
    14.  
    15.     void FixedUpdate() {
    16.         if (isLocalPlayer)
    17.         {
    18.             rb.AddForce( // moveVector from Update() );
    19.             syncPos = transform.position;
    20.         }
    21.         else
    22.             transform.position = syncPos;
    23.     }
    24. }
    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.
     
    Last edited: Sep 25, 2015
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    nordee likes this.
  3. nordee

    nordee

    Joined:
    Sep 25, 2015
    Posts:
    5
    Ah, that makes sense. That's why everyone uses a Command or a ClientCallback. You update the specific client's SyncVar, and then the server takes care of distributing it to all the other clients.

    Thank you.