Search Unity

Sending phone rotation via a SyncVar, I must be missing something here?

Discussion in 'Multiplayer' started by Theformand, May 22, 2016.

  1. Theformand

    Theformand

    Joined:
    Jan 2, 2010
    Posts:
    271
    Hey all

    I'm trying to make a small project where a phone build should send its device orientation to the editor, to simulate VR headmovement. Its just to have an ease-of-use way of testing builds without putting your phone in the google cardboard, gearvr or whatever.

    I decided to try and send the device orientation via a Vector3 SyncVar, but nothing happens on the receiving end. I set up the phone to act as a server, and the editor to act as a client (SyncVars go from server->client).

    I double checked the device gyro output, and it reads just fine on the phone build, but isnt coming across the network. I just tagged my orientation variable with [SyncVar] and the each frame I update that value with the gyro readings.
    I see the "Connected to server" message, so I'm assuming theres an actual connection.

    Another solution would be to use OnSerialize/OnDeserialize, but the OnSerialize function doesnt seem to exist? Using Unity 5.3.4.
    What am I missing here?

    code below

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class MyNetworkManager : NetworkBehaviour {
    6.  
    7.     public bool isAtStartup = true;
    8.     private bool serverMode = false;
    9.  
    10.     private bool serverRunning = false;
    11.  
    12.     NetworkClient myClient;
    13.  
    14.     [SyncVar(hook ="OnRotChange")]
    15.     public Vector3 phoneOrientation;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.  
    20.         Input.gyro.enabled = true;
    21.  
    22.     }
    23.  
    24.     void OnRotChange(Vector3 rot)
    25.     {
    26.         Debug.Log("new value: "+rot);
    27.     }
    28.  
    29.  
    30.  
    31.     // Update is called once per frame
    32.     void Update () {
    33.  
    34.         if (isServer)
    35.         {
    36.             SyncPhoneRotationData();
    37.         }
    38.  
    39.     }
    40.  
    41.     void SyncPhoneRotationData()
    42.     {
    43.         phoneOrientation = Input.gyro.attitude.eulerAngles;    
    44.     }
    45.  
    46.     void OnGUI()
    47.     {
    48.         if (isAtStartup)
    49.         {
    50.             if (GUI.Button(new Rect(10,10,100,100),"Phone server"))
    51.             {
    52.                 SetupServer();
    53.             }
    54.  
    55.             if (GUI.Button(new Rect(10, 120, 100, 100), "Desktop client"))
    56.             {
    57.                 SetupClient();
    58.             }
    59.         }
    60.     }
    61.  
    62.     // Create a server and listen on a port
    63.     public void SetupServer()
    64.     {
    65.         NetworkServer.Listen(4444);
    66.         isAtStartup = false;
    67.         serverMode = true;
    68.     }
    69.  
    70.     // Create a client and connect to the server port
    71.     public void SetupClient()
    72.     {
    73.         myClient = new NetworkClient();
    74.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    75.         myClient.Connect("192.168.1.71", 4444);
    76.         isAtStartup = false;
    77.     }
    78.  
    79.     // Create a local client and connect to the local server
    80.     public void SetupLocalClient()
    81.     {
    82.         myClient = ClientScene.ConnectLocalServer();
    83.         myClient.RegisterHandler(MsgType.Connect, OnConnected);
    84.         isAtStartup = false;
    85.     }
    86.  
    87.     // client function
    88.     public void OnConnected(NetworkMessage netMsg)
    89.     {
    90.         Debug.Log("Connected to server");
    91.     }
    92. }
    93.  
     
  2. Theformand

    Theformand

    Joined:
    Jan 2, 2010
    Posts:
    271
    So it seems I need to add a [Command] method to trigger the sending of this. As what I'm trying to do is stream data every frame, I guess I should not use SyncVars and look into OnSerialize instead.