Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Network.Instantiate Only Creates Host's Character

Discussion in 'Multiplayer' started by KnightOfGamelot, Aug 8, 2014.

  1. KnightOfGamelot

    KnightOfGamelot

    Joined:
    Jul 12, 2013
    Posts:
    40
    Hey everyone,

    I just started learning about networking yesterday. I have a basic scene setup where I can create a room and have other players join it. The problem is that only the host's character shows up. When Player 2 joins he can see player 1's character moving around, but his does not exist.

    To help distinguish between different players, the players' personal character is blue. All other players' characters are yellow.

    Here is my Network Manager code: - (On an empty game object)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NetworkManager : MonoBehaviour {
    6.  
    7.     //Declare variables
    8.     private string typeGame = "NetworkGame";
    9.     private string gameName = "RoomName";
    10.     public GameObject playerPrefab;
    11.     public GameObject spawnPoint;
    12.     public int playerCount = 0;
    13.  
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         //MasterServer.ipAddress = "localhost";
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update () {
    22.  
    23.     }
    24.  
    25.     private void StartServer()
    26.     {
    27.         Network.InitializeServer (2, 23466, !Network.HavePublicAddress());
    28.         MasterServer.RegisterHost (typeGame, gameName);
    29.     }
    30.  
    31.     void OnServerInitialized()
    32.     {
    33.         Debug.Log ("[NETWORK]Server Initialized");
    34.         SpawnPlayer ();
    35.  
    36.     }
    37.  
    38.     //General User Interface
    39.     void OnGUI()
    40.     {
    41.         if (!Network.isClient && !Network.isServer)
    42.         {
    43.             if (GUI.Button (new Rect(100,100,250, 100), "Start Server"))
    44.                 StartServer();
    45.             if (GUI.Button(new Rect(100, 250, 250, 100), "Refresh Hosts"))
    46.                 RefreshHostList();
    47.             if(hostList != null)
    48.             {
    49.                 for(int i = 0; i < hostList.Length; i++)
    50.                 {
    51.                 if(GUI.Button (new Rect(400, 100+(110*i), 300, 100), hostList[i].gameName))
    52.                     JoinServer(hostList[i]);
    53.                 }
    54.             }
    55.         }
    56.     }
    57.  
    58.     //Host Information
    59.  
    60.     private HostData[] hostList;
    61.  
    62.     private void RefreshHostList()
    63.     {
    64.         MasterServer.RequestHostList(typeGame);
    65.     }
    66.     void OnMasterServerEvent(MasterServerEvent msEvent)
    67.     {
    68.         if(msEvent == MasterServerEvent.HostListReceived)
    69.         {
    70.             hostList = MasterServer.PollHostList();
    71.         }
    72.     }
    73.  
    74.     private void JoinServer(HostData hostData)
    75.     {
    76.         Network.Connect (hostData);
    77.     }
    78.     void OnConnecterToServer()
    79.     {
    80.         Debug.Log ("[NETWORK]Server Joined");
    81.         SpawnPlayer ();
    82.     }
    83.  
    84.     private void SpawnPlayer()
    85.     {
    86.         Network.Instantiate(playerPrefab, new Vector3(990f, 5f, 990f), Quaternion.identity, 0);
    87.         //Network.Instantiate (playerPrefab, new Vector3 (spawnPoint.transform.position.x, spawnPoint.transform.position.y, spawnPoint.transform.position.z), Quaternion.identity, 0);
    88.         Debug.Log ("[NETWORK]playerName has joined the game.");
    89.         playerCount += 1;
    90.     }
    91.  
    92. }
    93.  
    Here is the player script: - (on players character)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerScript : MonoBehaviour {
    6.  
    7.     //Define Variables
    8.     public int speed = 25;
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.  
    14.     }
    15.  
    16.     //If player is mine, turn blue; if someone else, display as yellow
    17.     void Update () {
    18.         if (networkView.isMine) {
    19.             InputMovement ();
    20.             gameObject.renderer.material.color = new Color (0, 0, 255);
    21.                 } else
    22.                     {
    23.                     SyncedMovement();
    24.                     gameObject.renderer.material.color = new Color(255,255,0);
    25.                     }
    26.     }
    27.  
    28.  
    29.     //Player Movement
    30.  
    31.     void InputMovement()
    32.     {
    33.         if (Input.GetKey (KeyCode.W))
    34.             rigidbody.MovePosition (rigidbody.position - Vector3.forward * speed * Time.deltaTime);
    35.         if (Input.GetKey (KeyCode.S))
    36.             rigidbody.MovePosition (rigidbody.position + Vector3.forward * speed * Time.deltaTime);
    37.         if (Input.GetKey (KeyCode.D))
    38.             rigidbody.MovePosition (rigidbody.position - Vector3.right * speed * Time.deltaTime);
    39.         if (Input.GetKey (KeyCode.A))
    40.             rigidbody.MovePosition (rigidbody.position + Vector3.right * speed * Time.deltaTime);
    41.     }
    42.  
    43.     //This section is for syncing the players and minimizing the effects of latency
    44.     //Interpolation
    45.     private float lastSyncronizationTime = 0f;
    46.     private float syncDelay = 0f;
    47.     private float syncTime = 0f;
    48.     private Vector3 syncStartPosition = Vector3.zero;
    49.     private Vector3 syncEndPosition = Vector3.zero;
    50.  
    51.     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
    52.     {
    53.         Vector3 syncPosition = Vector3.zero;
    54.         Vector3 syncVelocity = Vector3.zero;
    55.         if (stream.isWriting)
    56.             {
    57.             syncPosition = rigidbody.position;
    58.             stream.Serialize (ref syncPosition);
    59.  
    60.             syncVelocity = rigidbody.velocity;
    61.             stream.Serialize(ref syncVelocity);
    62.             } else
    63.                 {
    64.             stream.Serialize(ref syncPosition);
    65.            
    66.             syncTime = 0f;
    67.             syncDelay = Time.time - lastSyncronizationTime;
    68.             lastSyncronizationTime = Time.time;
    69.  
    70.             syncEndPosition = syncPosition + syncVelocity * syncDelay;
    71.             syncStartPosition = rigidbody.position;
    72.  
    73.                 }
    74.     }
    75.  
    76.     private void SyncedMovement()
    77.     {
    78.         syncTime += Time.deltaTime;
    79.         rigidbody.position = Vector3.Lerp (syncStartPosition, syncEndPosition, syncTime / syncDelay);
    80.     }
    81.  
    82.  
    83. }
    84.  
    85.  
    An image to demonstrate my scene has been linked below.
    networkIssueImage.png
    networkIssueImage.png
     
  2. KnightOfGamelot

    KnightOfGamelot

    Joined:
    Jul 12, 2013
    Posts:
    40
    I am still looking for an answer. There has to be someone who knows how to solve this. I incorporated a player count to the network manager. When player 2 joins the player count does not change(it stays at 1.)
     
  3. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Hahahahaha!
    "void OnConnecterToServer()"
    You made a typo bud :'P
    it should be
    "void OnConnectedToServer()"

    Very amusing how people waste so much time on such simple mistakes! x3
     
    KnightOfGamelot likes this.
  4. KnightOfGamelot

    KnightOfGamelot

    Joined:
    Jul 12, 2013
    Posts:
    40
    Wow! I hate myself now lol! I'm surprised that I didn't get a syntax error. Or is this not part of a library and just a function that I created?
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Well, it was just a function that you created. Because it wasn't spelled correctly :'P
     
  6. KnightOfGamelot

    KnightOfGamelot

    Joined:
    Jul 12, 2013
    Posts:
    40
    I guess I never thought to look there because player 1 was spawning correctly, but player 1 was spawning from the OnServerInitialized() function.

    Thanks a lot.
     
    Magiichan likes this.