/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Traits { using Opsive.UltimateCharacterController.Utility; using UnityEngine; /// /// Applies an impulse to the Rigidbody when enabled. /// public class RigidbodyImpulse : MonoBehaviour { [Tooltip("Should the force be applied in the local space?")] [SerializeField] protected bool m_LocalForce = true; [Tooltip("The force to apply to the Rigidbody.")] [SerializeField] protected MinMaxVector3 m_Force = new MinMaxVector3(new Vector3(0, 5, 0), new Vector3(0, 5, 0)); [Tooltip("Should the torque be applied in the local space?")] [SerializeField] protected bool m_LocalTorque = true; [Tooltip("The torque to apply to the Rigidbody.")] [SerializeField] protected MinMaxVector3 m_Torque; private Rigidbody m_Rigidbody; /// /// Initialize the default values. /// private void Awake() { m_Rigidbody = GetComponent(); } /// /// Add the impulse force. /// private void OnEnable() { if (m_LocalForce) { m_Rigidbody.AddRelativeForce(m_Force.RandomValue, ForceMode.Impulse); } else { m_Rigidbody.AddForce(m_Force.RandomValue, ForceMode.Impulse); } if (m_LocalTorque) { m_Rigidbody.AddRelativeTorque(m_Torque.RandomValue); } else { m_Rigidbody.AddTorque(m_Torque.RandomValue); } } } }