diff --git a/Assets/Mirror/Examples/AdditiveLevels/Scripts/Portal.cs b/Assets/Mirror/Examples/AdditiveLevels/Scripts/Portal.cs index 77a81b372..f64427a2b 100644 --- a/Assets/Mirror/Examples/AdditiveLevels/Scripts/Portal.cs +++ b/Assets/Mirror/Examples/AdditiveLevels/Scripts/Portal.cs @@ -27,7 +27,7 @@ public void OnLabelTextChanged(string _, string newValue) public override void OnStartServer() { - labelText = Path.GetFileNameWithoutExtension(destinationScene).Replace("MirrorAdditiveLevels",""); + labelText = Path.GetFileNameWithoutExtension(destinationScene).Replace("MirrorAdditiveLevels", ""); // Simple Regex to insert spaces before capitals, numbers labelText = Regex.Replace(labelText, @"\B[A-Z0-9]+", " $0"); @@ -60,42 +60,41 @@ void OnTriggerEnter(Collider other) [ServerCallback] IEnumerator SendPlayerToNewScene(GameObject player) { - if (player.TryGetComponent(out NetworkIdentity identity)) - { - NetworkConnectionToClient conn = identity.connectionToClient; - if (conn == null) yield break; + if (!player.TryGetComponent(out NetworkIdentity identity)) yield break; - // Tell client to unload previous subscene with custom handling (see NetworkManager::OnClientChangeScene). - conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true }); + NetworkConnectionToClient conn = identity.connectionToClient; + if (conn == null) yield break; - // wait for fader to complete. - yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetFadeInTime()); + // Tell client to unload previous subscene with custom handling (see NetworkManager::OnClientChangeScene). + conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true }); - // Remove player after fader has completed - NetworkServer.RemovePlayerForConnection(conn, RemovePlayerOptions.Unspawn); + // wait for fader to complete. + yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetFadeInTime()); - // reposition player on server and client - player.transform.position = startPosition; + // Remove player after fader has completed + NetworkServer.RemovePlayerForConnection(conn, RemovePlayerOptions.Unspawn); - // Rotate player to face center of scene - // Player is 2m tall with pivot at 0,1,0 so we need to look at - // 1m height to not tilt the player down to look at origin - player.transform.LookAt(Vector3.up); + // reposition player on server and client + player.transform.position = startPosition; - // Move player to new subscene. - SceneManager.MoveGameObjectToScene(player, SceneManager.GetSceneByPath(destinationScene)); + // Rotate player to face center of scene + // Player is 2m tall with pivot at 0,1,0 so we need to look at + // 1m height to not tilt the player down to look at origin + player.transform.LookAt(Vector3.up); - // Tell client to load the new subscene with custom handling (see NetworkManager::OnClientChangeScene). - conn.Send(new SceneMessage { sceneName = destinationScene, sceneOperation = SceneOperation.LoadAdditive, customHandling = true }); + // Move player to new subscene. + SceneManager.MoveGameObjectToScene(player, SceneManager.GetSceneByPath(destinationScene)); - // Player will be spawned after destination scene is loaded - NetworkServer.AddPlayerForConnection(conn, player); + // Tell client to load the new subscene with custom handling (see NetworkManager::OnClientChangeScene). + conn.Send(new SceneMessage { sceneName = destinationScene, sceneOperation = SceneOperation.LoadAdditive, customHandling = true }); - // host client playerController would have been disabled by OnTriggerEnter above - // Remote client players are respawned with playerController already enabled - if (NetworkClient.localPlayer != null && NetworkClient.localPlayer.TryGetComponent(out Common.Controllers.Player.PlayerControllerBase playerController)) - playerController.enabled = true; - } + // Player will be spawned after destination scene is loaded + NetworkServer.AddPlayerForConnection(conn, player); + + // host client playerController would have been disabled by OnTriggerEnter above + // Remote client players are respawned with playerController already enabled + if (NetworkClient.localPlayer != null && NetworkClient.localPlayer.TryGetComponent(out Common.Controllers.Player.PlayerControllerBase playerController)) + playerController.enabled = true; } } }