Files
Stair_Horror/Assets/Scripts/TVRoomTrap.cs

69 lines
2.1 KiB
C#

using UnityEngine;
using System.Collections;
public class TVRoomTrap : MonoBehaviour
{
[Header("Warp Locations")]
public Transform tvRoomSpawn; // Where they get trapped (TV Room)
public Transform mainHallSpawn; // Where they escape to (Hallway)
[Header("Room Events")]
public HauntedTV tvScript; // Reference to the TV script
public HauntedPram pramScript; // Reference to the Pram script
[Tooltip("How long they are trapped in the room")]
public float trapDuration = 3.0f;
[Header("Audio")]
public AudioSource trapAudioSource;
public AudioClip warpInSound;
public AudioClip warpOutSound;
// Call this function to start the nightmare
public void TriggerTrap(GameObject player)
{
StartCoroutine(TrapSequence(player));
}
IEnumerator TrapSequence(GameObject player)
{
CharacterController cc = player.GetComponent<CharacterController>();
// 1. DISABLE CONTROLS & PHYSICS
// We disable the CC so it doesn't fight the teleportation
if (cc != null) cc.enabled = false;
// 2. WARP TO TV ROOM
player.transform.position = tvRoomSpawn.position;
player.transform.rotation = tvRoomSpawn.rotation;
// Play Entry Sound
if(trapAudioSource && warpInSound) trapAudioSource.PlayOneShot(warpInSound);
// 3. FORCE TV SCARE
if (tvScript != null && pramScript != null)
{
pramScript.StartAllSounds();
tvScript.StartEvent();
}
// 4. WAIT (The 2 seconds of panic)
yield return new WaitForSeconds(trapDuration);
// 5. WARP TO MAIN HALL
player.transform.position = mainHallSpawn.position;
player.transform.rotation = mainHallSpawn.rotation;
// 6. FORCE TV SCARE
if (tvScript != null && pramScript != null)
{
pramScript.StopAllSounds();
tvScript.StopEvent();
}
// Play Exit Sound
if(trapAudioSource && warpOutSound) trapAudioSource.PlayOneShot(warpOutSound);
// 7. RE-ENABLE CONTROLS
if (cc != null) cc.enabled = true;
}
}