SetVelocity2d.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "Sets the 2d Velocity of a Game Object. To leave any axis unchanged, set variable to 'None'. NOTE: Game object must have a rigidbody 2D."
  9. )]
  10. public class SetVelocity2d : ChronosComponentAction<Timeline>
  11. {
  12. [RequiredField]
  13. [CheckForComponent(typeof(Timeline))]
  14. public FsmOwnerDefault gameObject;
  15. [UIHint(UIHint.Variable)]
  16. public FsmVector2 vector;
  17. public FsmFloat x;
  18. public FsmFloat y;
  19. public bool everyFrame;
  20. public override void Reset()
  21. {
  22. gameObject = null;
  23. vector = null;
  24. // default axis to variable dropdown with None selected.
  25. x = new FsmFloat { UseVariable = true };
  26. y = new FsmFloat { UseVariable = true };
  27. everyFrame = false;
  28. }
  29. public override void Awake()
  30. {
  31. Fsm.HandleFixedUpdate = true;
  32. }
  33. // TODO: test this works in OnEnter!
  34. public override void OnEnter()
  35. {
  36. DoSetVelocity();
  37. if (!everyFrame)
  38. {
  39. Finish();
  40. }
  41. }
  42. public override void OnFixedUpdate()
  43. {
  44. DoSetVelocity();
  45. if (!everyFrame)
  46. Finish();
  47. }
  48. private void DoSetVelocity()
  49. {
  50. if (!UpdateCache(Fsm.GetOwnerDefaultTarget(gameObject))) return;
  51. // init position
  52. Vector2 velocity;
  53. if (vector.IsNone)
  54. {
  55. velocity = timeline.rigidbody2D.velocity;
  56. }
  57. else
  58. {
  59. velocity = vector.Value;
  60. }
  61. // override any axis
  62. if (!x.IsNone) velocity.x = x.Value;
  63. if (!y.IsNone) velocity.y = y.Value;
  64. // apply
  65. timeline.rigidbody2D.velocity = velocity;
  66. }
  67. }
  68. }
  69. #endif