Files
CartoonCopChase/Assets/Scripts/SafeHouseTrigger.cs

59 lines
1.8 KiB
C#

using UnityEngine;
using System.Collections;
using ArcadeVP;
public class SafeHouseTrigger : MonoBehaviour
{
[Tooltip("Optional: Drag your GarageDoor script here so it shuts behind you when you park.")]
public GarageDoor garageDoor;
private bool triggered = false;
private void OnTriggerEnter(Collider other)
{
// Ignore if the game is already over or we already triggered this
if (triggered || GameManager.Instance == null || GameManager.Instance.isGameOver) return;
ArcadeVehicleController vehicle = null;
// Check if the object entering the trigger is the Player directly
if (other.CompareTag("Player"))
{
vehicle = other.GetComponent<ArcadeVehicleController>();
if (vehicle == null) vehicle = other.GetComponentInParent<ArcadeVehicleController>();
}
else
{
// Also check if a child collider of the player entered
vehicle = other.GetComponentInParent<ArcadeVehicleController>();
}
if (vehicle != null && vehicle.CompareTag("Player"))
{
triggered = true;
StartCoroutine(WaitToStopAndClose(vehicle));
}
}
private IEnumerator WaitToStopAndClose(ArcadeVehicleController vehicle)
{
// Wait until the car has essentially stopped moving (speed < 1 m/s)
while (vehicle.carBody != null && vehicle.carBody.linearVelocity.magnitude > 1f)
{
yield return null;
}
// Shut the garage door!
if (garageDoor != null)
{
garageDoor.CloseDoor();
// Wait 2.5 seconds for the door to visually close before ending the level
yield return new WaitForSeconds(2.5f);
}
// Complete the level
GameManager.Instance.EnterSafeHouse();
}
}