using UnityEngine;
using UnityEngine.Events;
using System.Collections;
using System.Collections.Generic;
public class UniTrigger : MonoBehaviour
{
public List<string> tags = new List<string> {"Player"};
[Min(0)]
public float exitDelay = 0;
private float counter = 0;
public float startDelay = 0;
public UnityEvent startEvents;
public UnityEvent stayEvents;
[Min(0)]
public UnityEvent exitEvents;
public bool isRepetable = false;
private bool isStayCompleted = false;
public float stayTime = 3;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
void OnTriggerStay(Collider other)
{
if (tags.Contains(other.tag))
{
if (counter >= stayTime && !isStayCompleted)
{
stayEvents.Invoke();
if(isRepetable)
{
counter=0;
}
else
{
isStayCompleted = true;
}
}
}
}
// Update is called once per frame
void Update()
{
}
IEnumerator DoFuncWithDelay(float delay, UnityEvent _event)
{
yield return new WaitForSeconds(delay);
_event.Invoke();
}
void OnTriggerEnter(Collider other)
{
if(tags.Contains(other.tag))
{
StartCoroutine(DoFuncWithDelay(startDelay, startEvents));
counter = 0;
}
}
void OnTriggerExit(Collider other)
{
if (tags.Contains(other.tag))
{
StartCoroutine(DoFuncWithDelay(exitDelay, exitEvents));
counter = 0;
}
}
}