AniDriver_MoveByPath.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ChivaXR;
  5. using System;
  6. using DG.Tweening;
  7. using Sirenix.OdinInspector;
  8. using QFramework;
  9. using UnityEngine.PlayerLoop;
  10. public class AniDriver_MoveByPath : AnimationDriverBase
  11. {
  12. [LabelText("打开路径")]
  13. public bool openLinePath;
  14. [LabelText("需要移动对象")]
  15. public Transform movePlayer;
  16. [LabelText("移动路径点")]
  17. public List<Transform> pathPoints = new List<Transform>();
  18. //public float moveSpeed = 4;
  19. //public float rotateSpeed = 4;
  20. public float duration;
  21. public override void FinishedState()
  22. {
  23. movePlayer.position = pathPoints[pathPoints.Count - 1].position;
  24. }
  25. public override void InitState()
  26. {
  27. movePlayer.position = pathPoints[0].position;
  28. }
  29. public override void StartPlay(Action finishedCallBack = null)
  30. {
  31. StartAniCoroutine(MovePathByItem(finishedCallBack));
  32. }
  33. public override void OnDrawGizmos()
  34. {
  35. if (!openLinePath) return;
  36. if (pathPoints == null || pathPoints.Count <= 1) return;
  37. for (int i = 0; i < pathPoints.Count; i++)
  38. {
  39. if (i + 1 < pathPoints.Count)
  40. {
  41. Gizmos.color = Color.red;
  42. Gizmos.DrawLine(pathPoints[i].position, pathPoints[i + 1].position);
  43. }
  44. }
  45. }
  46. ///// <summary>
  47. ///// 根据路径点移动
  48. ///// </summary>
  49. ///// <param name="points"></param>
  50. ///// <param name="moveObj"></param>
  51. ///// <param name="moveSpeed"></param>
  52. ///// <param name="rotateSpeed"></param>
  53. ///// <returns></returns>
  54. //IEnumerator MovePathByItem(Action finishedCallBack = null)
  55. //{
  56. // for (int i = 0; i < pathPoints.Count; i++)
  57. // {
  58. // bool moveToTarget = false;
  59. // while (!moveToTarget)
  60. // {
  61. // movePlayer.transform.position = Vector3.MoveTowards(movePlayer.transform.position, pathPoints[i].position, moveSpeed * Time.deltaTime);
  62. // movePlayer.transform.rotation = Quaternion.Lerp(movePlayer.transform.rotation, pathPoints[i].rotation, rotateSpeed * Time.deltaTime);
  63. // if (Vector3.Distance(movePlayer.transform.position, pathPoints[i].position) < 0.02f && Quaternion.Angle(movePlayer.transform.rotation, pathPoints[i].rotation) < 5)
  64. // {
  65. // movePlayer.transform.position = pathPoints[i].position;
  66. // movePlayer.transform.rotation = pathPoints[i].rotation;
  67. // moveToTarget = true;
  68. // }
  69. // yield return new WaitForEndOfFrame();
  70. // }
  71. // }
  72. // Debug.Log("MoveEnd is true");
  73. // finishedCallBack?.Invoke();
  74. //}
  75. /// <summary>
  76. /// 根据路径点移动
  77. /// </summary>
  78. /// <param name="points"></param>
  79. /// <param name="moveObj"></param>
  80. /// <param name="moveSpeed"></param>
  81. /// <param name="rotateSpeed"></param>
  82. /// <returns></returns>
  83. IEnumerator MovePathByItem(Action finishedCallBack = null)
  84. {
  85. //总距离
  86. float tmpTotalLength = 0;
  87. for (int i = 0; i < pathPoints.Count - 1; i++) tmpTotalLength += Vector3.Distance(pathPoints[i].position, pathPoints[i + 1].position);
  88. //平均时间 = 总时长/总距离
  89. float averageTime = duration / tmpTotalLength;
  90. for (int i = 0; i < pathPoints.Count - 1; i++)
  91. {
  92. //初始位置
  93. Vector3 tmpInitPosition = pathPoints[i].position;
  94. //初始角度
  95. Quaternion tmpInitQuaternion = pathPoints[i].rotation;
  96. bool moveToTarget = false;
  97. float timer = 0;
  98. //移动指定距离所需时间
  99. float tmpTime = Vector3.Distance(pathPoints[i].position, pathPoints[i + 1].position)*averageTime;
  100. float lerp = 0;
  101. while (!moveToTarget)
  102. {
  103. movePlayer.transform.position = Vector3.Lerp(tmpInitPosition, pathPoints[i + 1].position, lerp);
  104. movePlayer.transform.rotation = Quaternion.Lerp(tmpInitQuaternion, pathPoints[i + 1].rotation, lerp);
  105. if (Vector3.Distance(movePlayer.transform.position, pathPoints[i + 1].position) < 0.02f && Quaternion.Angle(movePlayer.transform.rotation, pathPoints[i + 1].rotation) < 5)
  106. {
  107. movePlayer.transform.position = pathPoints[i + 1].position;
  108. movePlayer.transform.rotation = pathPoints[i + 1].rotation;
  109. moveToTarget = true;
  110. }
  111. timer += Time.deltaTime;
  112. lerp = timer / tmpTime;
  113. yield return new WaitForEndOfFrame();
  114. }
  115. }
  116. Debug.Log("MoveEnd is true");
  117. finishedCallBack?.Invoke();
  118. }
  119. }