39 lines
922 B
C#
39 lines
922 B
C#
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.Events;
|
||
|
|
|
||
|
|
public class Interact : MonoBehaviour, IInteract
|
||
|
|
{
|
||
|
|
[SerializeField] protected GameObject triggerObject;
|
||
|
|
|
||
|
|
[Header("Events")]
|
||
|
|
[SerializeField] protected UnityEvent onPlayerEnter;
|
||
|
|
[SerializeField] protected bool triggerOnce = false;
|
||
|
|
protected bool hasBeenTriggered = false;
|
||
|
|
private void OnTriggerEnter(Collider other)
|
||
|
|
{
|
||
|
|
if (!other.CompareTag("Player"))
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (triggerOnce && hasBeenTriggered)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
hasBeenTriggered = true;
|
||
|
|
OnPlayerTriggerEnter(other);
|
||
|
|
onPlayerEnter?.Invoke();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Make this virtual so subclasses can extend it.
|
||
|
|
protected virtual void OnPlayerTriggerEnter(Collider other)
|
||
|
|
{
|
||
|
|
// Base behavior here.
|
||
|
|
}
|
||
|
|
|
||
|
|
public virtual void OnInteract()
|
||
|
|
{
|
||
|
|
// Base interaction behavior
|
||
|
|
}
|
||
|
|
}
|