AddForce2d.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #if CHRONOS_PLAYMAKER
  2. using HutongGames.PlayMaker;
  3. using UnityEngine;
  4. namespace Chronos.PlayMaker
  5. {
  6. [ActionCategory("Physics 2d (Chronos)")]
  7. [HutongGames.PlayMaker.Tooltip(
  8. "Adds a 2d force to a Game Object. Use Vector2 variable and/or Float variables for each axis.")]
  9. public class AddForce2d : ChronosComponentAction<Timeline>
  10. {
  11. [RequiredField]
  12. [CheckForComponent(typeof(Timeline))]
  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 GetCollision2dInfo actions."
  18. )]
  19. public FsmVector2 atPosition;
  20. [UIHint(UIHint.Variable)]
  21. [HutongGames.PlayMaker.Tooltip("A Vector2 force to add. Optionally override any axis with the X, Y parameters.")]
  22. public FsmVector2 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("A Vector3 force to add. z is ignored")]
  28. public FsmVector3 vector3;
  29. [HutongGames.PlayMaker.Tooltip("Repeat every frame while the state is active.")]
  30. public bool everyFrame;
  31. public override void Reset()
  32. {
  33. gameObject = null;
  34. atPosition = new FsmVector2 { UseVariable = true };
  35. vector = null;
  36. vector3 = new FsmVector3 { UseVariable = true };
  37. // default axis to variable dropdown with None selected.
  38. x = new FsmFloat { UseVariable = true };
  39. y = new FsmFloat { UseVariable = true };
  40. everyFrame = false;
  41. }
  42. public override void Awake()
  43. {
  44. Fsm.HandleFixedUpdate = true;
  45. }
  46. public override void OnEnter()
  47. {
  48. DoAddForce();
  49. if (!everyFrame)
  50. {
  51. Finish();
  52. }
  53. }
  54. public override void OnFixedUpdate()
  55. {
  56. DoAddForce();
  57. }
  58. private void DoAddForce()
  59. {
  60. if (!UpdateCache(Fsm.GetOwnerDefaultTarget(gameObject))) return;
  61. Vector2 force = vector.IsNone ? new Vector2(x.Value, y.Value) : vector.Value;
  62. if (!vector3.IsNone)
  63. {
  64. force.x = vector3.Value.x;
  65. force.y = vector3.Value.y;
  66. }
  67. // override any axis
  68. if (!x.IsNone) force.x = x.Value;
  69. if (!y.IsNone) force.y = y.Value;
  70. // apply force
  71. if (!atPosition.IsNone)
  72. {
  73. timeline.rigidbody2D.AddForceAtPosition(force, atPosition.Value);
  74. }
  75. else
  76. {
  77. timeline.rigidbody2D.AddForce(force);
  78. }
  79. }
  80. }
  81. }
  82. #endif