Search Unity

Sync variable of spawned object

Discussion in 'Multiplayer' started by LabMatti, Jun 1, 2018.

  1. LabMatti

    LabMatti

    Joined:
    Jan 19, 2018
    Posts:
    58
    Hi!

    I've 2 problems syncing a video.

    This is my script:
    Code (CSharp):
    1. public class videoSync : NetworkBehaviour {
    2.  
    3.     [SyncVar]
    4.     public double videoTime;
    5.     public VideoPlayer video;
    6.  
    7.     void Start(){
    8.         video = GetComponent<VideoPlayer> ();
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.         if (isServer) {
    15.             videoTime = video.time;
    16.         } else if (isClient) {
    17.             double difference = videoTime - video.time +0.5f;
    18.             if (difference <= -0.001) {
    19.                 video.playbackSpeed = 1.2f;
    20.             } else if (difference >= 0.001) {
    21.                 video.playbackSpeed = 0.8f;
    22.             } else {
    23.                 video.playbackSpeed = 1f;
    24.             }
    25.             if (difference > 0.001 || difference < -0.01) {
    26.                 video.time = videoTime;
    27.             }
    28.  
    29.         }
    30.     }
    31. }
    32.  
    1) Clients can't sync the server, they are delayed. But between clients there is a perfect sync.
    How can I sync client and server without delay?

    2)This is my main problem. If I spawn a video with this script attached, it doesn't sync.
    What can I do?

    thank you!
     
    Last edited: Jun 4, 2018
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What variable are you trying to sync? I don't see any networking code other than checking if this is a server or client.
     
  3. LabMatti

    LabMatti

    Joined:
    Jan 19, 2018
    Posts:
    58
    Sorry I edited the code. The variable that I'm trying to sync is "videoTime"
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Syncing a SyncVar isn't instantaneous. The server will always have the value of videoTime before the clients - that's just the nature of client/server networking. You could have the server tell the clients a specific network time to act on any updates, so the clients use the value of videoTime at the same time the server does.