using System.Collections; using UnityEngine; using UnityEngine.Rendering.Universal; using UnityEngine.UI; // Handle room and scene transitions public class TeleportHandler : MonoBehaviour { PlayerController playerController; private void Start() { playerController = GetComponent(); } /// /// Triggers the room entrance coroutine, only works if the character is not controllable (keeps from double transitioning) /// public void EnterRoom(RoomDoor door) { if (playerController.GetCharacterHasControl()) StartCoroutine(EnterRoomCoroutine(door)); } /// /// Handle teleportation and timing for entering rooms within a scene /// IEnumerator EnterRoomCoroutine(RoomDoor door) { float fadeDuration = 0.2f; // how long to fade to/from black float moveDuration = 0.4f; // how long to auto walk into room from the door float fadeTime = 0; Color blackScreenColor = Color.black; playerController.SetCharacterControl(false); // Play door open animation if (door.requiresInteraction) yield return new WaitForSeconds(0.2f); while (fadeTime < fadeDuration) { blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration); GameManager.Instance.GetBlackScreen().color = blackScreenColor; fadeTime += Time.deltaTime; yield return null; } fadeTime = 0; playerController.CharacterControllerMove(door.linkedDoor.transform.position - transform.position); playerController.DougBody.transform.rotation = door.linkedDoor.transform.rotation; while (fadeTime < fadeDuration) { blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration); GameManager.Instance.GetBlackScreen().color = blackScreenColor; playerController.WalkInDirection((door.linkedDoor.WalkDirection.position - playerController.transform.position).normalized); fadeTime += Time.deltaTime; yield return null; } // move character a little more while (fadeTime < moveDuration) { playerController.WalkInDirection((door.linkedDoor.WalkDirection.position - playerController.transform.position).normalized); fadeTime += Time.deltaTime; yield return null; } fadeTime = 0; // Play exit animation for linked door (if it exists) door.linkedDoor.CloseDoor(); yield return new WaitForSeconds(0.2f); playerController.SetCharacterControl(true); } }