12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using ChivaXR;
- using System;
- public class AniDriver_MoveByPathItem : AnimationDriverBase
- {
- public MovePathItem mMovePathItem;
- [Header("反向播放")]
- public bool reverse = false;
- public override void FinishedState()
- {
- mMovePathItem.SetFinishedState(reverse);
- }
- public override void InitState()
- {
- mMovePathItem.SetInitState(reverse);
- }
- public override void StartPlay(Action finishedCallBack = null)
- {
- StartAniCoroutine(MovePathByItem(mMovePathItem, reverse, finishedCallBack));
- }
- /// <summary>
- /// 根据路径点移动
- /// </summary>
- /// <param name="points"></param>
- /// <param name="moveObj"></param>
- /// <param name="moveSpeed"></param>
- /// <param name="rotateSpeed"></param>
- /// <returns></returns>
- IEnumerator MovePathByItem(MovePathItem movePathItem, bool isReverse, Action finishedCallBack = null)
- {
- List<Transform> points = movePathItem.GetMovePathPoints(isReverse);
- yield return new WaitForSeconds(movePathItem.waitTime);
- for (int i = 0; i < points.Count; i++)
- {
- bool moveToTarget = false;
- while (!moveToTarget)
- {
- movePathItem.moveTarget.transform.position = Vector3.MoveTowards(movePathItem.moveTarget.transform.position, points[i].position, movePathItem.moveSpeed * Time.deltaTime);
- movePathItem.moveTarget.transform.rotation = Quaternion.Lerp(movePathItem.moveTarget.transform.rotation, points[i].rotation, movePathItem.rotateSpeed * Time.deltaTime);
- if (Vector3.Distance(movePathItem.moveTarget.transform.position, points[i].position) < 0.02f && Quaternion.Angle(movePathItem.moveTarget.transform.rotation, points[i].rotation) < 5)
- {
- movePathItem.moveTarget.transform.position = points[i].position;
- movePathItem.moveTarget.transform.rotation = points[i].rotation;
- moveToTarget = true;
- }
- yield return new WaitForEndOfFrame();
- }
- }
- Debug.Log("MoveEnd is true");
- finishedCallBack?.Invoke();
- }
- }
|