using System.Collections; using System.Collections.Generic; using UnityEngine; namespace MoreMountains.Tools { /// /// Add this class to a Rigidbody or Rigidbody2D to be able to edit its center of mass from the inspector directly /// public class MMRigidbodyCenterOfMass : MonoBehaviour { /// the possible modes this class can start on public enum AutomaticSetModes { Awake, Start, ScriptOnly } [Header("CenterOfMass")] /// the offset to apply to the center of mass public Vector3 CenterOfMassOffset; [Header("Automation")] /// whether to set the center of mass on awake, start, or via script only public AutomaticSetModes AutomaticSetMode = AutomaticSetModes.Awake; /// whether or not this component should auto destroy after a set public bool AutoDestroyComponentAfterSet = true; [Header("Test")] /// the size of the gizmo point to display at the center of mass public float GizmoPointSize = 0.05f; /// a button to test the set method [MMInspectorButton("SetCenterOfMass")] public bool SetCenterOfMassButton; protected Vector3 _gizmoCenter; protected Rigidbody _rigidbody; protected Rigidbody2D _rigidbody2D; /// /// On Awake we grab our components and set our center of mass if needed /// protected virtual void Awake() { Initialization(); if (AutomaticSetMode == AutomaticSetModes.Awake) { SetCenterOfMass(); } } /// /// On Start we set our center of mass if needed /// protected virtual void Start() { if (AutomaticSetMode == AutomaticSetModes.Start) { SetCenterOfMass(); } } /// /// Grabs the rigidbody or rigidbody2D components /// protected virtual void Initialization() { _rigidbody = this.gameObject.MMGetComponentNoAlloc(); _rigidbody2D = this.gameObject.MMGetComponentNoAlloc(); } /// /// Sets the center of mass on the rigidbody or rigidbody2D /// public virtual void SetCenterOfMass() { if (_rigidbody != null) { _rigidbody.centerOfMass = CenterOfMassOffset; } if (_rigidbody2D != null) { _rigidbody2D.centerOfMass = CenterOfMassOffset; } if (AutoDestroyComponentAfterSet) { Destroy(this); } } /// /// On DrawGizmosSelected, we draw a yellow point at the position of our center of mass /// protected virtual void OnDrawGizmosSelected() { _gizmoCenter = this.transform.TransformPoint(CenterOfMassOffset); MMDebug.DrawGizmoPoint(_gizmoCenter, GizmoPointSize, Color.yellow); } } }