2022-12-10 02:24:32 +01:00
|
|
|
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;
|
|
|
|
|
2022-12-10 02:24:32 +01:00
|
|
|
// 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;
|
2022-12-10 02:24:32 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-12-10 02:24:32 +01:00
|
|
|
}
|
|
|
|
}
|