83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
//============================================================
|
||
//支持中文,文件使用UTF-8编码
|
||
//@author Adam
|
||
//@create 20240522
|
||
//@company Aadm
|
||
//
|
||
//@description:
|
||
//============================================================
|
||
namespace Components
|
||
{
|
||
public class NPCWaypoints : MonoBehaviour
|
||
{
|
||
public Transform[] waypoints; // 路径点数组
|
||
private int currentWaypoint = 0; // 当前路径点索引
|
||
private float moveSpeed = 1f; // 移动速度
|
||
public Animator npcAnimator;
|
||
public bool isMove = false;
|
||
public bool isOnce = false;
|
||
public bool isLooked = true;
|
||
public TextMeshPro customTip;
|
||
// Use this for initialization
|
||
private void Start()
|
||
{
|
||
for (int i = 0; i < waypoints.Length; i++)
|
||
{
|
||
waypoints[i].GetComponent<MeshRenderer>().enabled = false;
|
||
}
|
||
customTip = GetComponentInChildren<TextMeshPro>();
|
||
customTip.text = $"客户";
|
||
}
|
||
private void Update()
|
||
{
|
||
if (!isMove) return;
|
||
if (currentWaypoint < waypoints.Length)
|
||
{
|
||
float distance = Vector3.Distance(transform.position, waypoints[currentWaypoint].position);
|
||
if (distance > 0.1f)
|
||
{
|
||
if (isLooked)
|
||
{
|
||
transform.LookAt(waypoints[currentWaypoint]);
|
||
isLooked = false;
|
||
}
|
||
Vector3 direction = waypoints[currentWaypoint].position - transform.position;
|
||
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
|
||
}
|
||
else
|
||
{
|
||
currentWaypoint++;
|
||
isLooked = true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
|
||
if (isMove && !isOnce)
|
||
{
|
||
npcAnimator.SetBool("isWalk", false);
|
||
isMove = false;
|
||
isOnce = true;
|
||
}
|
||
}
|
||
}
|
||
//[ContextMenu("StartMove")]
|
||
public void StartMove()
|
||
{
|
||
StartCoroutine(WaitMove());
|
||
}
|
||
|
||
private IEnumerator WaitMove()
|
||
{
|
||
yield return new WaitForSeconds(3f);
|
||
npcAnimator.SetBool("isWalk", true);
|
||
isMove = true;
|
||
isOnce = false;
|
||
}
|
||
}
|
||
}
|