FlowTorque.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. using CW.Common;
  3. namespace FLOW
  4. {
  5. /// <summary>This component applies continuous torque to the sibling <b>Rigidbody</b> component.</summary>
  6. [RequireComponent(typeof(Rigidbody))]
  7. [HelpURL(FlowCommon.HelpUrlPrefix + "FlowTorque")]
  8. [AddComponentMenu(FlowCommon.ComponentMenuPrefix + "Torque")]
  9. public class FlowTorque : MonoBehaviour
  10. {
  11. /// <summary>This allows you to specify a positional offset relative to the <b>Target</b>.</summary>
  12. public Vector3 Torque { set { torque = value; } get { return torque; } } [SerializeField] private Vector3 torque;
  13. /// <summary>The frame of reference the torque will be applied in.</summary>
  14. public Space Space { set { space = value; } get { return space; } } [SerializeField] private Space space;
  15. /// <summary>The force mode the torque will be applied using.</summary>
  16. public ForceMode ForceMode { set { forceMode = value; } get { return forceMode; } } [SerializeField] private ForceMode forceMode;
  17. [System.NonSerialized]
  18. private Rigidbody cachedRigidbody;
  19. protected virtual void OnEnable()
  20. {
  21. cachedRigidbody = GetComponent<Rigidbody>();
  22. }
  23. protected virtual void FixedUpdate()
  24. {
  25. if (space == Space.Self)
  26. {
  27. cachedRigidbody.AddRelativeTorque(torque * Time.fixedDeltaTime, forceMode);
  28. }
  29. else
  30. {
  31. cachedRigidbody.AddTorque(torque * Time.fixedDeltaTime, forceMode);
  32. }
  33. }
  34. }
  35. }
  36. #if UNITY_EDITOR
  37. namespace FLOW
  38. {
  39. using UnityEditor;
  40. using TARGET = FlowTorque;
  41. [CanEditMultipleObjects]
  42. [CustomEditor(typeof(TARGET))]
  43. public class FlowTorque_Editor : CwEditor
  44. {
  45. protected override void OnInspector()
  46. {
  47. TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts);
  48. Draw("torque", "This allows you to specify a positional offset relative to the <b>Target</b>.");
  49. Draw("space", "The frame of reference the torque will be applied in.");
  50. Draw("forceMode", "The force mode the torque will be applied using.");
  51. }
  52. }
  53. }
  54. #endif