Первый коммит с LFS.
This commit is contained in:
1 parent
4fcb1e2f24
commit
179816dc4b
5084 files changed
+3714098
-1
No files matched your search
@@ -0,0 +1,51 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user