using UnityEngine; using FSA = UnityEngine.Serialization.FormerlySerializedAsAttribute; namespace Lean.Common { /// This component rotates the current GameObject based on the current Angle value. /// NOTE: This component overrides and takes over the rotation of this GameObject, so you can no longer externally influence it. [ExecuteInEditMode] [HelpURL(LeanHelper.HelpUrlPrefix + "LeanRoll")] [AddComponentMenu(LeanHelper.ComponentPathPrefix + "Roll")] public class LeanRoll : MonoBehaviour { /// The current angle in degrees. public float Angle; /// Should the Angle value be clamped? public bool Clamp; /// The minimum Angle value. public float ClampMin; /// The maximum Angle value. public float ClampMax; /// If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value. /// -1 = Instantly change. /// 1 = Slowly change. /// 10 = Quickly change. [FSA("Dampening")] public float Damping = - 1.0f; [SerializeField] private float currentAngle; /// The Angle value will be incremented by the specified angle in degrees. public void IncrementAngle(float delta) { Angle += delta; } /// The Angle value will be decremented by the specified angle in degrees. public void DecrementAngle(float delta) { Angle -= delta; } /// This method will update the Angle value based on the specified vector. public void RotateToDelta(Vector2 delta) { if (delta.sqrMagnitude > 0.0f) { Angle = Mathf.Atan2(delta.x, delta.y) * Mathf.Rad2Deg; } } /// This method will immediately snap the current angle to its target value. [ContextMenu("Snap To Target")] public void SnapToTarget() { currentAngle = Angle; } protected virtual void Start() { currentAngle = Angle; } protected virtual void Update() { // Get t value var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime); if (Clamp == true) { Angle = Mathf.Clamp(Angle, ClampMin, ClampMax); } // Lerp angle currentAngle = Mathf.LerpAngle(currentAngle, Angle, factor); // Update rotation transform.rotation = Quaternion.Euler(0.0f, 0.0f, -currentAngle); } } } #if UNITY_EDITOR namespace Lean.Common.Inspector { using UnityEditor; [CanEditMultipleObjects] [CustomEditor(typeof(LeanRoll))] public class LeanRoll_Inspector : LeanInspector { private bool showUnusedEvents; protected override void DrawInspector() { Draw("Angle", "The current angle in degrees."); Draw("Clamp", "Should the Angle value be clamped?"); if (Any(t => t.Clamp == true)) { EditorGUI.indentLevel++; Draw("ClampMin", "The minimum Angle value.", "Min"); Draw("ClampMax", "The maximum Angle value.", "Max"); EditorGUI.indentLevel--; EditorGUILayout.Separator(); } Draw("Damping", "If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value.\n\n-1 = Instantly change.\n\n1 = Slowly change.\n\n10 = Quickly change."); } } } #endif