Afterparty/Assets/Scripts/Events/EventTriggerOnStay.cs
2026-01-11 17:04:23 +03:00

51 lines
1.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Collider))]
public class EventTriggerOnStay : MonoBehaviour
{
[Tooltip("Тег объекта, который должен активировать триггер.")]
[SerializeField] private string _targetTag = "Player";
[Tooltip("Событие, которое будет вызвано.")]
[SerializeField] private GameEvent _eventToRaise;
[Tooltip("Время в секундах, которое объект должен провести в триггере для срабатывания.")]
[SerializeField] private float _timeToStay = 2.0f;
[Tooltip("Если true, событие будет срабатывать только один раз.")]
[SerializeField] private bool _triggerOnce = true;
private bool _hasBeenTriggered = false;
private Coroutine _timerCoroutine;
private void OnTriggerEnter(Collider other)
{
if ((_hasBeenTriggered && _triggerOnce) || !other.CompareTag(_targetTag))
{
return;
}
// Запускаем корутину-таймер
_timerCoroutine = StartCoroutine(TimerCoroutine());
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag(_targetTag) && _timerCoroutine != null)
{
// Если игрок покинул зону, останавливаем таймер
StopCoroutine(_timerCoroutine);
_timerCoroutine = null;
}
}
private IEnumerator TimerCoroutine()
{
yield return new WaitForSeconds(_timeToStay);
_eventToRaise.Raise();
_hasBeenTriggered = true;
_timerCoroutine = null;
}
}