GetSpeed.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #if CHRONOS_PLAYMAKER
  2. using HutongGames.PlayMaker;
  3. namespace Chronos.PlayMaker
  4. {
  5. [ActionCategory("Physics (Chronos)")]
  6. [Tooltip(
  7. "Gets the Speed of a Game Object and stores it in a Float Variable. NOTE: The Game Object must have a rigid body.")]
  8. public class GetSpeed : ChronosComponentAction<Timeline>
  9. {
  10. [RequiredField]
  11. [CheckForComponent(typeof(Timeline))]
  12. [Tooltip("The GameObject with a Rigidbody.")]
  13. public
  14. FsmOwnerDefault gameObject;
  15. [RequiredField]
  16. [UIHint(UIHint.Variable)]
  17. [Tooltip("Store the speed in a float variable.")]
  18. public FsmFloat
  19. storeResult;
  20. [Tooltip("Repeat every frame.")]
  21. public bool everyFrame;
  22. public override void Reset()
  23. {
  24. gameObject = null;
  25. storeResult = null;
  26. everyFrame = false;
  27. }
  28. public override void OnEnter()
  29. {
  30. DoGetSpeed();
  31. if (!everyFrame)
  32. {
  33. Finish();
  34. }
  35. }
  36. public override void OnUpdate()
  37. {
  38. DoGetSpeed();
  39. }
  40. private void DoGetSpeed()
  41. {
  42. if (storeResult == null)
  43. {
  44. return;
  45. }
  46. var go = gameObject.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : gameObject.GameObject.Value;
  47. if (UpdateCache(go))
  48. {
  49. var velocity = timeline.rigidbody.velocity;
  50. storeResult.Value = velocity.magnitude;
  51. }
  52. }
  53. }
  54. }
  55. #endif