代码之家  ›  专栏  ›  技术社区  ›  Siddharth

10秒后,玩家在Unity多人游戏中生成

  •  0
  • Siddharth  · 技术社区  · 6 年前

    目前在我的多人游戏中,我的两个玩家都是在游戏开始时生成的,但我想在等待10秒后生成他们。

    因为我想在游戏中提供这种功能,所以我想给玩家一些时间。

    enter image description here

    Custom Player Spawning

    这样我可以更新播放机,然后与网络连接仍然存在两个问题:

    • 无法等待10秒,因为callback get会自动调用-OnServerAddPlayer

    我需要为这些做些什么? 以下是我的网络管理器代码:

    public class DodgelsNetworkManager : NetworkManager
    {
    
     public override void OnClientConnect (NetworkConnection connection)
     {
         base.OnClientConnect (connection);
    
         GameHUDController.Instance.UpdateDebugMesssage ("\nOnClientConnect");
    
         Camera.main.SendMessage (GameConstants.ACTIVATE_NETWORK_WAITING_PANEL, true, SendMessageOptions.DontRequireReceiver);
         LayerScroller.stopScrolling = true;
    
         // client joined the host
         if (connection.connectionId > 0) {
    
             StartCoroutine (AfterDelayHideWaitingDialog ()); 
         }
     }
    
     IEnumerator AfterDelayHideWaitingDialog ()
     {
         GameObject networkPlayerObj = null;
         while (networkPlayerObj == null) {
             yield return new WaitForSeconds (0.1f);
             networkPlayerObj = GameObject.FindGameObjectWithTag (GameConstants.TAG_HUMAN_PLAYER);
         }
    
         networkPlayerObj.GetComponent<NetworkPlayerController> ().HidePlayerWaitingDialog ();
     }
    
     public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId)
     {
         Debug.Log ("-------------OnServerAddPlayer");
         GameObject player = Instantiate (playerPrefab, startPositions [conn.connectionId].position, Quaternion.identity);
         NetworkServer.AddPlayerForConnection (conn, player, playerControllerId);
     }
    
    }
    

    分享你对此的建议。

    1 回复  |  直到 6 年前
        1
  •  1
  •   slaphshot33324    6 年前

    在OnServerAddPlayer中调用另一个子例程,该子例程在实例化播放器之前等待10秒。

     if (manager.IsClientConnected() && !ClientScene.ready)
     {
         ClientScene.Ready(manager.client.connection);
         ClientScene.AddPlayer(0);
     }