Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Player only visible on server

Discussion in 'Multiplayer' started by Royall, Jun 16, 2015.

  1. Royall

    Royall

    Joined:
    Jun 15, 2013
    Posts:
    120
    So I am trying to create a Server/Client setup without using the NetworkManager as I like to have more control over it... Aside from the outdated documentation at the moment I created this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. public class Controller : MonoBehaviour {
    4.     NetworkClient myClient = new NetworkClient();
    5.     public GameObject playerPrefab;
    6.    
    7.     void startServer() {
    8.         NetworkServer.RegisterHandler(MsgType.AddPlayer, OnServerAddPlayer);
    9.         NetworkServer.RegisterHandler(MsgType.Disconnect, OnServerDisconnect);
    10.         NetworkServer.Listen(7070);
    11.         Debug.Log("Server started...");
    12.     }
    13.     void connectServer() {
    14.         myClient.RegisterHandler(MsgType.Connect, OnClientConnected);
    15.         myClient.Connect("127.0.0.1", 7070);
    16.     }
    17.     void OnClientConnected(NetworkMessage mess) {
    18.         Debug.Log("Connected: "+mess.conn.connectionId);
    19.         ClientScene.Ready(mess.conn);
    20.         ClientScene.AddPlayer (0);
    21.     }
    22.     void OnServerDisconnect(NetworkMessage mess) {
    23.         Debug.Log("Disconnected: "+mess.conn.connectionId);
    24.         NetworkServer.DestroyPlayersForConnection(mess.conn);
    25.     }
    26.     void OnServerAddPlayer(NetworkMessage mess) {
    27.         Debug.Log("Add player: "+mess.conn);
    28.         GameObject thePlayer = (GameObject)Instantiate(playerPrefab, new Vector3(0,0,0), Quaternion.identity);
    29.         NetworkServer.AddPlayerForConnection(mess.conn, thePlayer, 0);
    30.     }
    31.     void OnGUI() {
    32.         //Server GUI
    33.         if(!myClient.isConnected && !NetworkServer.active) {
    34.             if(GUI.Button(new Rect(10, 30, 120, 30), "Start server")) {
    35.                 startServer();
    36.             }
    37.         } else if(!myClient.isConnected) {
    38.             if(GUI.Button(new Rect(10, 30, 120, 30), "Stop server")) {
    39.                 NetworkServer.Shutdown();
    40.                 Debug.Log("Server stopped...");
    41.             }
    42.         }
    43.        
    44.         //Client GUI
    45.         if(!myClient.isConnected && !NetworkServer.active) {
    46.             if(GUI.Button(new Rect(10, 80, 120, 30), "Connect server")) {
    47.                 connectServer();
    48.             }
    49.         } else if(!NetworkServer.active) {
    50.             if(GUI.Button(new Rect(10, 30, 120, 30), "Disconnect")) {
    51.                 myClient.Disconnect();
    52.                 Debug.Log("Client disconnected...");
    53.             }
    54.         }
    55.     }
    56. }
    Starting the server and connecting works. But the player prefab only instantiates on the server. Not on the client.

    Do I miss something?
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    You need to register the player prefab to be spawnable with the ClientScene.

    Code (CSharp):
    1.     void connectServer() {
    2.         ClientScene.RegisterPrefab(playerPrefab);
    3.         myClient.RegisterHandler(MsgType.Connect, OnClientConnected);
    4.         myClient.Connect("127.0.0.1", 7070);
    5.     }
     
  3. Royall

    Royall

    Joined:
    Jun 15, 2013
    Posts:
    120
    Nice I was missing that one line :O
     
  4. Royall

    Royall

    Joined:
    Jun 15, 2013
    Posts:
    120
    Do you have any more tips on this script?
     
  5. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    It looks like it is a good start.

    The hardest thing that the NetworkManager does is to manage the timing of scene transitions and spawning across the server and clients. That will be something your code will have to deal with too if you have scene changes.
     
  6. Royall

    Royall

    Joined:
    Jun 15, 2013
    Posts:
    120
    Ok thanks! Another one :)

    I have added an network transform to the player prefab... The position wont update if I have local player authorization checked on the network indentity component of the player. But with a dedicated server you don't want this I guess... Do you know why this doesnt work without?