using System.Collections; using System.Collections.Generic; using MLAPI; using MLAPI.Messaging; using UnityEngine; public class GameHandler : NetworkBehaviour { public Transform coundownUI; public Transform playerHUD; private static GameObject _countdownUI; private static GameObject _playerHUD; // Start is called before the first frame update void Start() { StartCoroutine(introCutscene()); } IEnumerator introCutscene() { // Get the time to complete the intro camera fly through float cameraLerpTime = Camera.main.GetComponent().playOnAwakeTime; // Wait for that many seconds - allows for time to complete the "cutscene" yield return new WaitForSecondsRealtime(cameraLerpTime); // Remove the main camera and give camera to local players Camera.main.gameObject.SetActive(false); // Get all players in the scene - runners and king GameObject[] playableCharacters = GameObject.FindGameObjectsWithTag("Player"); // Iterate over them and if their network object equals the call to the rpc, enable that camera GameObject localPlayer = null; foreach (GameObject character in playableCharacters) { if (character.GetComponent().OwnerClientId == NetworkManager.Singleton.LocalClientId) { localPlayer = character; if(FindGameObjectInChildWithTag(character, "UICam") != null){ FindGameObjectInChildWithTag(character, "UICam").GetComponent().enabled = true; GameObject UICamera = FindGameObjectInChildWithTag(character, "UICam"); FindGameObjectInChildWithTag(UICamera, "PlayerCam").GetComponent().enabled = true; FindGameObjectInChildWithTag(UICamera, "PlayerCam").GetComponent().enabled = true; if (FindGameObjectInChildWithTag(UICamera, "PlayerCam").GetComponent() != null) { FindGameObjectInChildWithTag(UICamera, "PlayerCam").GetComponent().enabled = true; } } else{ FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent().enabled = true; FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent().enabled = true; if (FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent() != null) { FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent().enabled = true; } } } } // Spawn the 3.2.1 coundown object if (IsHost) { SpawnCountdownServerRpc(); } yield return new WaitForSecondsRealtime(5f); // Despawn the 3.2.1 coundown object if (IsHost) { DespawnCountdownServerRpc(); } // Re-enable the runner movement if (localPlayer.GetComponentInChildren() != null) { localPlayer.GetComponentInChildren().enabled = true; } if (localPlayer.GetComponentInChildren() != null) { localPlayer.GetComponentInChildren().enabled = true; } if (localPlayer.GetComponentInChildren() != null) { localPlayer.GetComponentInChildren().enabled = true; } if (localPlayer.GetComponentInChildren() != null) { localPlayer.GetComponentInChildren().enabled = true; } if (localPlayer.GetComponentInChildren() != null) { localPlayer.GetComponentInChildren().enabled = true; } // Re-enable the kings movement if (localPlayer.GetComponent() != null) { localPlayer.GetComponent().enabled = true; } // Start the game timer and spawn HUDs if (IsHost) { SpawnPlayerHUDServerRpc(); } } // Only works for 1st generation children public static GameObject FindGameObjectInChildWithTag(GameObject parent, string tag) { Transform t = parent.transform; for (int i = 0; i < t.childCount; i++) { if (t.GetChild(i).gameObject.tag == tag) { return t.GetChild(i).gameObject; } } // Couldn't find child with tag return null; } [ServerRpc] private void SpawnCountdownServerRpc(ServerRpcParams serverRpcParams = default) { _countdownUI = Instantiate(coundownUI, Vector3.zero, Quaternion.identity).gameObject; _countdownUI.GetComponent().Spawn(null, true); } [ServerRpc] private void DespawnCountdownServerRpc(ServerRpcParams serverRpcParams = default) { _countdownUI.GetComponent().Despawn(true); Destroy(_countdownUI); } [ServerRpc] private void SpawnPlayerHUDServerRpc(ServerRpcParams serverRpcParams = default) { _playerHUD = Instantiate(playerHUD, Vector3.zero, Quaternion.identity).gameObject; _playerHUD.GetComponent().Spawn(null, true); } }