DOTweenModulePhysics2D.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2018/07/13
  3. #if true && (UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_2017_1_OR_NEWER) // MODULE_MARKER
  4. using System;
  5. using DG.Tweening.Core;
  6. using DG.Tweening.Plugins.Options;
  7. using UnityEngine;
  8. #pragma warning disable 1591
  9. namespace DG.Tweening
  10. {
  11. public static class DOTweenModulePhysics2D
  12. {
  13. #region Shortcuts
  14. #region Rigidbody2D Shortcuts
  15. /// <summary>Tweens a Rigidbody2D's position to the given value.
  16. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations</summary>
  17. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  18. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  19. public static TweenerCore<Vector2, Vector2, VectorOptions> DOMove(this Rigidbody2D target, Vector2 endValue, float duration, bool snapping = false)
  20. {
  21. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.position, target.MovePosition, endValue, duration);
  22. t.SetOptions(snapping).SetTarget(target);
  23. return t;
  24. }
  25. /// <summary>Tweens a Rigidbody2D's X position to the given value.
  26. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations</summary>
  27. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  28. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  29. public static TweenerCore<Vector2, Vector2, VectorOptions> DOMoveX(this Rigidbody2D target, float endValue, float duration, bool snapping = false)
  30. {
  31. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.position, target.MovePosition, new Vector2(endValue, 0), duration);
  32. t.SetOptions(AxisConstraint.X, snapping).SetTarget(target);
  33. return t;
  34. }
  35. /// <summary>Tweens a Rigidbody2D's Y position to the given value.
  36. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations</summary>
  37. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  38. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  39. public static TweenerCore<Vector2, Vector2, VectorOptions> DOMoveY(this Rigidbody2D target, float endValue, float duration, bool snapping = false)
  40. {
  41. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.position, target.MovePosition, new Vector2(0, endValue), duration);
  42. t.SetOptions(AxisConstraint.Y, snapping).SetTarget(target);
  43. return t;
  44. }
  45. /// <summary>Tweens a Rigidbody2D's rotation to the given value.
  46. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations</summary>
  47. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  48. public static TweenerCore<float, float, FloatOptions> DORotate(this Rigidbody2D target, float endValue, float duration)
  49. {
  50. TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.rotation, target.MoveRotation, endValue, duration);
  51. t.SetTarget(target);
  52. return t;
  53. }
  54. #region Special
  55. /// <summary>Tweens a Rigidbody2D's position to the given value, while also applying a jump effect along the Y axis.
  56. /// Returns a Sequence instead of a Tweener.
  57. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations.
  58. /// <para>IMPORTANT: a rigidbody2D can't be animated in a jump arc using MovePosition, so the tween will directly set the position</para></summary>
  59. /// <param name="endValue">The end value to reach</param>
  60. /// <param name="jumpPower">Power of the jump (the max height of the jump is represented by this plus the final Y offset)</param>
  61. /// <param name="numJumps">Total number of jumps</param>
  62. /// <param name="duration">The duration of the tween</param>
  63. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  64. public static Sequence DOJump(this Rigidbody2D target, Vector2 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
  65. {
  66. if (numJumps < 1) numJumps = 1;
  67. float startPosY = 0;
  68. float offsetY = -1;
  69. bool offsetYSet = false;
  70. Sequence s = DOTween.Sequence();
  71. Tween yTween = DOTween.To(() => target.position, x => target.position = x, new Vector2(0, jumpPower), duration / (numJumps * 2))
  72. .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad).SetRelative()
  73. .SetLoops(numJumps * 2, LoopType.Yoyo)
  74. .OnStart(() => startPosY = target.position.y);
  75. s.Append(DOTween.To(() => target.position, x => target.position = x, new Vector2(endValue.x, 0), duration)
  76. .SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)
  77. ).Join(yTween)
  78. .SetTarget(target).SetEase(DOTween.defaultEaseType);
  79. yTween.OnUpdate(() => {
  80. if (!offsetYSet) {
  81. offsetYSet = true;
  82. offsetY = s.isRelative ? endValue.y : endValue.y - startPosY;
  83. }
  84. Vector3 pos = target.position;
  85. pos.y += DOVirtual.EasedValue(0, offsetY, yTween.ElapsedPercentage(), Ease.OutQuad);
  86. target.MovePosition(pos);
  87. });
  88. return s;
  89. }
  90. #endregion
  91. #endregion
  92. #endregion
  93. }
  94. }
  95. #endif