GetVelocity2d.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "Gets the 2d Velocity of a Game Object and stores it in a Vector2 Variable or each Axis in a Float Variable. NOTE: The Game Object must have a Rigid Body 2D."
  9. )]
  10. public class GetVelocity2d : ChronosComponentAction<Timeline>
  11. {
  12. [RequiredField]
  13. [CheckForComponent(typeof(Timeline))]
  14. public FsmOwnerDefault gameObject;
  15. [UIHint(UIHint.Variable)]
  16. public FsmVector2 vector;
  17. [UIHint(UIHint.Variable)]
  18. public FsmFloat x;
  19. [UIHint(UIHint.Variable)]
  20. public FsmFloat y;
  21. public Space space;
  22. public bool everyFrame;
  23. public override void Reset()
  24. {
  25. gameObject = null;
  26. vector = null;
  27. x = null;
  28. y = null;
  29. space = Space.World;
  30. everyFrame = false;
  31. }
  32. public override void OnEnter()
  33. {
  34. DoGetVelocity();
  35. if (!everyFrame)
  36. Finish();
  37. }
  38. public override void OnUpdate()
  39. {
  40. DoGetVelocity();
  41. }
  42. private void DoGetVelocity()
  43. {
  44. if (!UpdateCache(Fsm.GetOwnerDefaultTarget(gameObject))) return;
  45. Vector2 velocity = timeline.rigidbody2D.velocity;
  46. if (space == Space.Self)
  47. velocity = timeline.gameObject.transform.InverseTransformDirection(velocity);
  48. vector.Value = velocity;
  49. x.Value = velocity.x;
  50. y.Value = velocity.y;
  51. }
  52. }
  53. }
  54. #endif