GetVelocity.cs 1.5 KB

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