Search Unity

[State Synchronization] Server Sends Data, Client Does Not

Discussion in 'Multiplayer' started by Cognations, Feb 20, 2014.

  1. Cognations

    Cognations

    Joined:
    Feb 19, 2014
    Posts:
    2
    First time trying networking in Unity. Love how easy they make it but still having bugs that bewilder me. Here's the current situation.

    After finding the server to connect to on the masterserver, the client calls this...

    Code (csharp):
    1.  
    2.     private void JoinServer(HostData hostData)
    3.     {
    4.         Network.Connect (hostData);
    5.         GameObject.Find ("player_type").tag = "slave";
    6.         Application.LoadLevel ("space!");
    7.     }
    8.  
    And the host calls this...

    Code (csharp):
    1.  
    2.     void OnPlayerConnected(NetworkPlayer player)
    3.     {
    4.         Application.LoadLevel ("space!");
    5.     }
    6.  
    They both load the level, so I assume they're connected.

    The level consists of three objects, an obstacle and two units. Each of the units is particular to each player, but otherwise is indistinguishable from the other. They both have a Network View component, Reliable Delta Compressed, and attached to a "update_pos" script on each of the objects respectively. The update_pos script looks like this...

    Code (csharp):
    1.  
    2.     private int packet_num = 0;
    3.     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
    4.     {
    5.         if (stream.isWriting) {
    6.                 Debug.Log ("Sending data: " + packet_num++);
    7.                 Vector3 pos = new Vector3();
    8.                 Quaternion rot = new Quaternion();
    9.                 pos = this.transform.position;
    10.                 rot = this.transform.rotation;
    11.                 stream.Serialize(ref pos);
    12.                 stream.Serialize(ref rot);
    13.         } else if (stream.isReading) {
    14.             Vector3 pos = new Vector3();
    15.             Quaternion rot = new Quaternion();
    16.             stream.Serialize (ref pos);
    17.             this.transform.position = pos;
    18.             stream.Serialize (ref rot);
    19.             this.transform.rotation = rot;
    20.         }
    21.     }
    22.  
    Basically when the server moves their unit, the unit moves around the map on both the client and the server. However, when the client moves their unit, it only moves around on the client. "Sending data" appears in the Console when the server moves, but not when the client moves. I even tried giving the client control of the server's unit, and still when the server moved it, it moved, when the client moved it, it didn't. It would even jump to wherever the server moved it to when the server moved it (as expected). I'm stumped. Any help? I told my team I would figure out basic networking by Friday, and this is the only thing stopping me from being there.
     
    Last edited: Feb 20, 2014
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,686
    The server seems to own the objects.
    Do you instantiate the player objects or are they already in the scene from the start?
    If that is the case, the server is the owner and only the server can move them.
     
  3. Cognations

    Cognations

    Joined:
    Feb 19, 2014
    Posts:
    2
    That definitely would be the problem! I'm now instantiating them on load, with this script...

    Code (csharp):
    1.  
    2. void Start () {
    3.         if (Network.isServer) {
    4.             Network.Instantiate(master, master.transform.position, master.transform.rotation, 0);
    5.         }
    6.         else if (Network.isClient) {
    7.             Network.Instantiate(slave, slave.transform.position, slave.transform.rotation, 0); 
    8.         }
    9.     }
    10.  
    But the client one doesn't show up at all! When I do both in Network.isServer, they both show up. Clearly I'd want to instantiate the client one on the client, but it doesn't seem to work... thoughts?

    EDIT: When I do this...

    Code (csharp):
    1.  
    2.     void Start () {
    3.             Network.Instantiate(master, master.transform.position, master.transform.rotation, 0);
    4.             Network.Instantiate(slave, slave.transform.position, slave.transform.rotation, 0); 
    5.     }
    6.  
    I get the same problem as before with the movement not working for the client. I should probably mention that this is on my camera.

    EDIT2:

    Debuging says that I'm not connected to the server... probably just jumping the gun is all... Trying that.
     
    Last edited: Feb 20, 2014
  4. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,686
    OnServerInitialized and OnConnectedToServer are both callbacks that can help you debug if your server is running and if your connected.
     
unityunity