AddForce.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #if CHRONOS_PLAYMAKER
  2. using HutongGames.PlayMaker;
  3. using UnityEngine;
  4. namespace Chronos.PlayMaker
  5. {
  6. [ActionCategory("Physics (Chronos)")]
  7. [HutongGames.PlayMaker.Tooltip(
  8. "Adds a force to a Game Object. Use Vector3 variable and/or Float variables for each axis.")]
  9. public class AddForce : ChronosComponentAction<Timeline>
  10. {
  11. [RequiredField]
  12. [CheckForComponent(typeof(RigidbodyTimeline3D))]
  13. [HutongGames.PlayMaker.Tooltip("The GameObject to apply the force to.")]
  14. public FsmOwnerDefault gameObject;
  15. [UIHint(UIHint.Variable)]
  16. [HutongGames.PlayMaker.Tooltip(
  17. "Optionally apply the force at a position on the object. This will also add some torque. The position is often returned from MousePick or GetCollisionInfo actions."
  18. )]
  19. public FsmVector3 atPosition;
  20. [UIHint(UIHint.Variable)]
  21. [HutongGames.PlayMaker.Tooltip("A Vector3 force to add. Optionally override any axis with the X, Y, Z parameters.")]
  22. public FsmVector3 vector;
  23. [HutongGames.PlayMaker.Tooltip("Force along the X axis. To leave unchanged, set to 'None'.")]
  24. public FsmFloat x;
  25. [HutongGames.PlayMaker.Tooltip("Force along the Y axis. To leave unchanged, set to 'None'.")]
  26. public FsmFloat y;
  27. [HutongGames.PlayMaker.Tooltip("Force along the Z axis. To leave unchanged, set to 'None'.")]
  28. public FsmFloat z;
  29. [HutongGames.PlayMaker.Tooltip("Apply the force in world or local space.")]
  30. public Space space;
  31. [HutongGames.PlayMaker.Tooltip("The type of force to apply. See Unity Physics docs.")]
  32. public ForceMode forceMode;
  33. [HutongGames.PlayMaker.Tooltip("Repeat every frame while the state is active.")]
  34. public bool everyFrame;
  35. public override void Reset()
  36. {
  37. gameObject = null;
  38. atPosition = new FsmVector3 { UseVariable = true };
  39. vector = null;
  40. // default axis to variable dropdown with None selected.
  41. x = new FsmFloat { UseVariable = true };
  42. y = new FsmFloat { UseVariable = true };
  43. z = new FsmFloat { UseVariable = true };
  44. space = Space.World;
  45. forceMode = ForceMode.Force;
  46. everyFrame = false;
  47. }
  48. public override void Awake()
  49. {
  50. Fsm.HandleFixedUpdate = true;
  51. }
  52. public override void OnEnter()
  53. {
  54. DoAddForce();
  55. if (!everyFrame)
  56. {
  57. Finish();
  58. }
  59. }
  60. public override void OnFixedUpdate()
  61. {
  62. DoAddForce();
  63. }
  64. private void DoAddForce()
  65. {
  66. var go = Fsm.GetOwnerDefaultTarget(gameObject);
  67. if (!UpdateCache(go))
  68. {
  69. return;
  70. }
  71. var force = vector.IsNone ? new Vector3(x.Value, y.Value, z.Value) : vector.Value;
  72. // override any axis
  73. if (!x.IsNone) force.x = x.Value;
  74. if (!y.IsNone) force.y = y.Value;
  75. if (!z.IsNone) force.z = z.Value;
  76. // apply force
  77. if (space == Space.World)
  78. {
  79. if (!atPosition.IsNone)
  80. {
  81. timeline.rigidbody.AddForceAtPosition(force, atPosition.Value, forceMode);
  82. }
  83. else
  84. {
  85. timeline.rigidbody.AddForce(force, forceMode);
  86. }
  87. }
  88. else
  89. {
  90. timeline.rigidbody.AddRelativeForce(force, forceMode);
  91. }
  92. }
  93. }
  94. }
  95. #endif