VfxActionMove.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using DG.Tweening;
  2. using UnityEngine;
  3. using XGame.Framework.Interfaces;
  4. namespace FL.Battle.Vfxs
  5. {
  6. internal class VfxActionMove : IVfxAction
  7. {
  8. public Vector3 from;
  9. public Vector3 to;
  10. /// <summary>
  11. /// 单位:毫秒
  12. /// </summary>
  13. public int duration;
  14. private Tween _tween;
  15. void IReference.Clear()
  16. {
  17. _tween?.Kill();
  18. _tween = null;
  19. }
  20. public void Start(Transform transform, bool isFoword, System.Action callback)
  21. {
  22. //transform.DOPlayForward();
  23. // 贝塞尔曲线运动
  24. transform.position = from;
  25. var timeScale = Random.Range(0.2f, 0.8f);
  26. var linePoint = Vector3.Lerp(from, to, timeScale);
  27. var dir = Vector3.Cross(to - from, Vector3.forward).normalized; //运动方向的法线
  28. // 沿Y轴上下运动时算出来z值为零,直接把x值赋值给z进行偏移
  29. if (dir.z == 0)
  30. dir.z = dir.x;
  31. var disOffset = Random.Range(-3f, 3f);
  32. var center = linePoint + dir * disOffset;
  33. var totalTime = duration * 0.001f;
  34. var timeTween = DOTween.To(() => 0, (t) =>
  35. {
  36. var tNormal = t / totalTime;
  37. transform.position = from.BezierPoint(center, to, tNormal);
  38. if (isFoword)
  39. {
  40. var tempDir = (to - transform.position).normalized;
  41. var rot = Vector3.zero;
  42. rot.z = Vector3.Angle(tempDir, Vector3.up);
  43. transform.localEulerAngles = rot;
  44. //if (tempDir != Vector3.zero)
  45. //{
  46. // transform.forward = tempDir;
  47. //}
  48. }
  49. }, totalTime, totalTime);
  50. timeTween.SetEase(Ease.OutQuad);
  51. _tween = timeTween;
  52. timeTween.onComplete = () =>
  53. {
  54. _tween = null;
  55. callback?.Invoke();
  56. };
  57. }
  58. void IVfxAction.Start(Transform transform)
  59. {
  60. Start(transform, true, null);
  61. }
  62. }
  63. }