SetVelocity.cs 1.9 KB

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