Search Unity

After 10 Seconds Player Spawning in Unity Multiplayer

Discussion in 'Multiplayer' started by siddharth3322, Oct 11, 2018.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    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.