using System; using UnityEngine.Events; namespace UnityEngine.XR.Content.Interaction { /// /// Calls functionality when a physics trigger occurs /// public class OnTrigger : MonoBehaviour { [Serializable] public class TriggerEvent : UnityEvent { } [SerializeField] [Tooltip("If set, this trigger will only fire if the other gameobject has this tag.")] string m_RequiredTag = string.Empty; [SerializeField] [Tooltip("Events to fire when a matcing object collides with this trigger.")] TriggerEvent m_OnEnter = new TriggerEvent(); [SerializeField] [Tooltip("Events to fire when a matching object stops colliding with this trigger.")] TriggerEvent m_OnExit = new TriggerEvent(); /// /// If set, this trigger will only fire if the other gameobject has this tag. /// public string requiredTag => m_RequiredTag; /// /// Events to fire when a matching object collides with this trigger. /// public TriggerEvent onEnter => m_OnEnter; /// /// Events to fire when a matching object stops colliding with this trigger. /// public TriggerEvent onExit => m_OnExit; void OnTriggerEnter(Collider other) { if (CanTrigger(other.gameObject)) m_OnEnter?.Invoke(other.gameObject); } void OnTriggerExit(Collider other) { if (CanTrigger(other.gameObject)) m_OnExit?.Invoke(other.gameObject); } void OnParticleCollision(GameObject other) { if (CanTrigger(other.gameObject)) m_OnEnter?.Invoke(other); } bool CanTrigger(GameObject otherGameObject) { if (m_RequiredTag != string.Empty) return otherGameObject.CompareTag(m_RequiredTag); else return true; } } }