AniDriver_MoveByPathItem.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ChivaXR;
  5. using System;
  6. public class AniDriver_MoveByPathItem : AnimationDriverBase
  7. {
  8. public MovePathItem mMovePathItem;
  9. [Header("反向播放")]
  10. public bool reverse = false;
  11. public override void FinishedState()
  12. {
  13. mMovePathItem.SetFinishedState(reverse);
  14. }
  15. public override void InitState()
  16. {
  17. mMovePathItem.SetInitState(reverse);
  18. }
  19. public override void StartPlay(Action finishedCallBack = null)
  20. {
  21. StartAniCoroutine(MovePathByItem(mMovePathItem, reverse, finishedCallBack));
  22. }
  23. /// <summary>
  24. /// 根据路径点移动
  25. /// </summary>
  26. /// <param name="points"></param>
  27. /// <param name="moveObj"></param>
  28. /// <param name="moveSpeed"></param>
  29. /// <param name="rotateSpeed"></param>
  30. /// <returns></returns>
  31. IEnumerator MovePathByItem(MovePathItem movePathItem, bool isReverse, Action finishedCallBack = null)
  32. {
  33. List<Transform> points = movePathItem.GetMovePathPoints(isReverse);
  34. yield return new WaitForSeconds(movePathItem.waitTime);
  35. for (int i = 0; i < points.Count; i++)
  36. {
  37. bool moveToTarget = false;
  38. while (!moveToTarget)
  39. {
  40. movePathItem.moveTarget.transform.position = Vector3.MoveTowards(movePathItem.moveTarget.transform.position, points[i].position, movePathItem.moveSpeed * Time.deltaTime);
  41. movePathItem.moveTarget.transform.rotation = Quaternion.Lerp(movePathItem.moveTarget.transform.rotation, points[i].rotation, movePathItem.rotateSpeed * Time.deltaTime);
  42. if (Vector3.Distance(movePathItem.moveTarget.transform.position, points[i].position) < 0.02f && Quaternion.Angle(movePathItem.moveTarget.transform.rotation, points[i].rotation) < 5)
  43. {
  44. movePathItem.moveTarget.transform.position = points[i].position;
  45. movePathItem.moveTarget.transform.rotation = points[i].rotation;
  46. moveToTarget = true;
  47. }
  48. yield return new WaitForEndOfFrame();
  49. }
  50. }
  51. Debug.Log("MoveEnd is true");
  52. finishedCallBack?.Invoke();
  53. }
  54. }