163 lines
3.9 KiB
C#
163 lines
3.9 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.AI;
|
||
|
||
public enum Npcstate
|
||
{
|
||
idle,
|
||
dead,
|
||
run
|
||
}
|
||
|
||
public class RecuseNpc : MonoBehaviour
|
||
{
|
||
private NavMeshAgent navMeshAgent;//navmesh组件
|
||
|
||
public string npcId;
|
||
|
||
public static RecuseNpc instance;
|
||
|
||
private Button recusebtn;
|
||
|
||
private bool statebool = false;
|
||
|
||
private Animator anim;
|
||
|
||
public Npcstate nstate = Npcstate.dead;
|
||
|
||
private bool movebool = false;
|
||
public Transform target;
|
||
|
||
public Vector3 currentTarget;
|
||
|
||
// <20>洢Ŀ<E6B4A2><C4BF><EFBFBD><EFBFBD>List
|
||
public List<Vector3> targetPoints = new List<Vector3>();
|
||
|
||
private void Awake()
|
||
{
|
||
instance = this;
|
||
recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
|
||
anim = this.GetComponent<Animator>();
|
||
|
||
navMeshAgent = GetComponent<NavMeshAgent>();
|
||
|
||
// 初始化 NavMeshAgent 的一些属性
|
||
navMeshAgent.speed = 3.5f; // 设置速度
|
||
navMeshAgent.acceleration = 8f; // 设置加速度
|
||
navMeshAgent.angularSpeed = 120f; // 设置旋转速度
|
||
navMeshAgent.stoppingDistance = 1f; // 设置停止的距离
|
||
|
||
}
|
||
|
||
private void OnTriggerEnter(Collider other)//ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>Ӧ<EFBFBD>ľ<EFBFBD>Ԯ<EFBFBD><D4AE><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1>ǩ<EFBFBD><C7A9><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ֳ<EFBFBD>UI
|
||
{
|
||
if(other.tag == "Player")
|
||
recusebtn.gameObject.SetActive(true);
|
||
if (statebool) return;
|
||
other.GetComponent<CharacterInturn>().cha = this.gameObject ;
|
||
}
|
||
|
||
private void OnTriggerExit(Collider other)
|
||
{
|
||
recusebtn.gameObject.SetActive(false);
|
||
}
|
||
|
||
public void SetNPCInfo(string id)
|
||
{
|
||
npcId = id;
|
||
}
|
||
|
||
//<2F><><EFBFBD><EFBFBD>NPC<50><43>״̬
|
||
public void Setnpcstate()//<2F><><EFBFBD><EFBFBD><EFBFBD>Ԯ<EFBFBD><D4AE>ťִ<C5A5><D6B4><EFBFBD>궯<EFBFBD><EAB6AF><EFBFBD><EFBFBD><EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
{
|
||
|
||
movebool = true;
|
||
nstate = Npcstate.run;
|
||
recusebtn.gameObject.SetActive(false);
|
||
Debug.Log("Setnpcstate<74><65><EFBFBD><EFBFBD>");
|
||
}
|
||
|
||
//<2F><><EFBFBD><EFBFBD>NPC<50><43>Ŀ<EFBFBD><C4BF><EFBFBD>
|
||
public void SetNpcDes(Vector3 tar)
|
||
{
|
||
//target.position = tar;
|
||
targetPoints.Add(tar); // <20><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5>б<EFBFBD><D0B1><EFBFBD>
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (targetPoints.Count > 0) // 确保列表中有目标点
|
||
{
|
||
currentTarget = targetPoints[0]; // 获取当前的目标点
|
||
|
||
// 判断是否到达目标点
|
||
if (movebool && navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
|
||
{
|
||
Debug.Log("到达目标点");
|
||
|
||
// 达到目标后,从列表中移除该目标点
|
||
if (targetPoints.Count > 1)
|
||
{
|
||
targetPoints = RemoveDes();
|
||
}
|
||
else
|
||
{
|
||
targetPoints.Clear(); // 清空列表
|
||
}
|
||
|
||
// 设置NPC状态
|
||
nstate = Npcstate.idle;
|
||
movebool = false;
|
||
}
|
||
}
|
||
|
||
// 继续处理NPC的状态和动画
|
||
switch (nstate)
|
||
{
|
||
case Npcstate.idle:
|
||
SetAni(2);
|
||
break;
|
||
case Npcstate.run:
|
||
movebool = true;
|
||
Run(currentTarget);
|
||
break;
|
||
case Npcstate.dead:
|
||
SetAni(0);
|
||
break;
|
||
}
|
||
}
|
||
|
||
//移除第一个点
|
||
public List<Vector3> RemoveDes()
|
||
{
|
||
List<Vector3> list = new List<Vector3>();
|
||
|
||
for(int i= 1;i<targetPoints.Count;i++)
|
||
{
|
||
list[i-1] = targetPoints[i];
|
||
}
|
||
|
||
return list;
|
||
}
|
||
|
||
|
||
public void Run(Vector3 target)
|
||
{
|
||
if (movebool)
|
||
{
|
||
SetAni(1);
|
||
navMeshAgent.SetDestination(target); // 使用 NavMeshAgent 设置目标点
|
||
}
|
||
}
|
||
|
||
|
||
public void SetAni(int x)
|
||
{
|
||
anim.SetInteger("state", x);
|
||
}
|
||
|
||
|
||
}
|