Scriptum/Assets/Scripts/Door/DetectionZone.cs

35 lines
879 B
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DetectionZone : MonoBehaviour
{
public string tagTarget = "Player";
2023-01-14 10:58:04 +01:00
public static bool ableToOpen = false;
// When object is detected, it is added to the list of actively detected objects
public List<Collider2D> detectedObjs = new List<Collider2D>();
// Detect when object enters range
void OnTriggerEnter2D(Collider2D collider) {
if(collider.gameObject.tag == tagTarget) {
2023-01-14 10:58:04 +01:00
ableToOpen = true;
detectedObjs.Add(collider);
}
}
// Detect when object leaves range
void OnTriggerExit2D(Collider2D collider) {
if(collider.gameObject.tag == tagTarget) {
detectedObjs.Remove(collider);
}
2023-01-14 10:58:04 +01:00
if(detectedObjs.Count == 0)
{
ableToOpen = false;
}
}
}