Search Unity

Need help with SyncVar

Discussion in 'Multiplayer' started by Cyrien5100, Jul 29, 2015.

  1. Cyrien5100

    Cyrien5100

    Joined:
    Oct 17, 2012
    Posts:
    145
    Hi,
    I'm trying to implement networking in my game, and i'm still learning about uNet.
    I'm doing a little tank game, and i'm trying to sync the rotation of turret of each player.
    My problem is that the turret's rotation of the other player sync correctly on client but not on host.
    It seems that the syncVar of the other player is not updated on host.
    I don't understand what is the problem.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetworkPlayer_SyncTurret : NetworkBehaviour {
    6.  
    7.     [SyncVar]
    8.     public Quaternion syncTurretRotation;
    9.     public Transform turret;
    10.  
    11.     void FixedUpdate () {
    12.         TransmitRotation();
    13.  
    14.         if(!isLocalPlayer){
    15.             turret.rotation = Quaternion.Lerp (turret.rotation,syncTurretRotation,Time.deltaTime*15);
    16.         }
    17.  
    18.     }
    19.  
    20.     void TransmitRotation(){
    21.         if(isLocalPlayer)syncTurretRotation = turret.rotation;
    22.     }
    23. }
    24.  
    Thanks for your help.
     
  2. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    Hi !

    A SyncVar is updated when the server change the value.
    In other words if you update the value from a client it will be not sync.