using UnityEngine.Events; namespace UnityEngine.XR.Content.Interaction { /// /// Calls events for when the velocity of this objects breaks the begin and end threshold. /// [RequireComponent(typeof(Rigidbody))] public class OnVelocity : MonoBehaviour { [SerializeField] [Tooltip("The speed that will trigger the begin event.")] float m_BeginThreshold = 1.25f; [SerializeField] [Tooltip("The speed that will trigger the end event.")] float m_EndThreshold = 0.25f; [SerializeField] [Tooltip("Event that triggers when speed meets the begin threshold.")] UnityEvent m_OnBegin = new UnityEvent(); [SerializeField] [Tooltip("Event that triggers when the speed dips below the end threshold.")] UnityEvent m_OnEnd = new UnityEvent(); /// /// Event that triggers when speed meets the begin threshold. /// public UnityEvent onBegin => m_OnBegin; /// /// Event that triggers when the speed dips below the end threshold. /// public UnityEvent onEnd => m_OnEnd; Rigidbody m_RigidBody; bool m_HasBegun; void Awake() { m_RigidBody = GetComponent(); } void Update() { CheckVelocity(); } void CheckVelocity() { var speed = m_RigidBody.velocity.magnitude; m_HasBegun = HasVelocityBegun(speed); if (HasVelocityEnded(speed)) m_HasBegun = false; } bool HasVelocityBegun(float speed) { if (m_HasBegun) return true; var beginCheck = speed > m_BeginThreshold; if (beginCheck) m_OnBegin.Invoke(); return beginCheck; } bool HasVelocityEnded(float speed) { if (!m_HasBegun) return false; var endCheck = speed < m_EndThreshold; if (endCheck) m_OnEnd.Invoke(); return endCheck; } } }