mycj_demo/mycj/Assets/Script/npcMove.cs
2024-12-02 09:37:47 +08:00

39 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class npcMove : MonoBehaviour
{
public List<Transform> path=new List<Transform>();
public float moveNeedTimer;
public void Init(List<Transform> path,float moveNeedTimer)
{
this.path = path;
this.moveNeedTimer = moveNeedTimer;
StartCoroutine(move());
}
IEnumerator move()
{
// 将路径点转换为 Vector3 数组
Vector3[] pathPoints = new Vector3[this.path.Count];
for (int i = 0; i < path.Count; i++)
{
pathPoints[i] = path[i].position;
}
// 使用 DOPath 沿着路径移动到目标点,并禁用旋转变化
yield return this.transform.DOPath(pathPoints, moveNeedTimer, PathType.Linear)
.SetOptions(false) // 禁用旋转
.SetEase(Ease.InOutSine)
.WaitForCompletion();
//移动到终点
PlayerInfo.instance.ChangeHp(-1);
Destroy(this.gameObject);
}
}