Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity Multiplayer After 10 Seconds Player Spawning in Unity Multiplayer

Discussion in 'Connected Games' started by siddharth3322, Oct 11, 2018.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    548
    I was using Unity's built-in multiplayer system for creating a multiplayer game and I have completed my whole game work.

    At present in my multiplayer game, my both players get spawned as the game get started but I want to spawn them after 10 seconds waiting.

    Because I want to provide this kind of functionality in a game so I want to give some time to players.

    random_match_maker.png

    For this reason, I have googled on multiple things and found this link:
    https://docs.unity3d.com/Manual/UNetPlayersCustom.html

    This way I can update the player and then attach with the network connection still two problems exist:

    • there was no way exist to give 10 seconds waiting because callback get called automatically - OnServerAddPlayer
    • OnServerAddPlayer method didn't get called automatically in a subclass of NetworkManager
    What do I require to do for these? Here is my NetworkManager code:

    Code (CSharp):
    1. public class DodgelsNetworkManager : NetworkManager
    2. {
    3.  
    4. public override void OnClientConnect (NetworkConnection connection)
    5. {
    6.      base.OnClientConnect (connection);
    7.  
    8.      GameHUDController.Instance.UpdateDebugMesssage ("\nOnClientConnect");
    9.  
    10.      Camera.main.SendMessage (GameConstants.ACTIVATE_NETWORK_WAITING_PANEL, true, SendMessageOptions.DontRequireReceiver);
    11.      LayerScroller.stopScrolling = true;
    12.  
    13.      // client joined the host
    14.      if (connection.connectionId > 0) {
    15.  
    16.          StartCoroutine (AfterDelayHideWaitingDialog ());
    17.      }
    18. }
    19.  
    20. IEnumerator AfterDelayHideWaitingDialog ()
    21. {
    22.      GameObject networkPlayerObj = null;
    23.      while (networkPlayerObj == null) {
    24.          yield return new WaitForSeconds (0.1f);
    25.          networkPlayerObj = GameObject.FindGameObjectWithTag (GameConstants.TAG_HUMAN_PLAYER);
    26.      }
    27.  
    28.      networkPlayerObj.GetComponent<NetworkPlayerController> ().HidePlayerWaitingDialog ();
    29. }
    30.  
    31. public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId)
    32. {
    33.      Debug.Log ("-------------OnServerAddPlayer");
    34.      GameObject player = Instantiate (playerPrefab, startPositions [conn.connectionId].position, Quaternion.identity);
    35.      NetworkServer.AddPlayerForConnection (conn, player, playerControllerId);
    36. }
    37.  
    38. }
    Share your suggestions regarding this.