_xiaofang/xiaofang/Assets/Script/npc/RecuseNpc.cs

170 lines
3.8 KiB
C#
Raw Normal View History

2024-10-21 18:09:07 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;
2024-10-22 11:57:50 +08:00
2024-10-22 17:37:42 +08:00
public enum Npcstate
{
idle,
dead,
2024-11-29 15:13:32 +08:00
run
2024-10-22 11:57:50 +08:00
}
2024-10-21 18:09:07 +08:00
public class RecuseNpc : MonoBehaviour
{
private NavMeshAgent navMeshAgent;//navmesh组件
2024-12-12 18:51:08 +08:00
public string npcId;
2024-12-09 09:46:52 +08:00
public static RecuseNpc instance;
public Button recusebtn;
2024-10-22 11:57:50 +08:00
2024-10-22 17:37:42 +08:00
private bool statebool = false;
private Animator anim;
2024-10-22 11:57:50 +08:00
2024-11-29 15:13:32 +08:00
public Npcstate nstate = Npcstate.dead;
2024-10-22 11:57:50 +08:00
2024-10-22 17:37:42 +08:00
private bool movebool = false;
public Transform target;
2024-12-12 14:50:53 +08:00
public Vector3 currentTarget;
// 存目的地的list
2024-12-12 14:50:53 +08:00
public List<Vector3> targetPoints = new List<Vector3>();
2024-10-22 11:57:50 +08:00
private void Awake()
2024-10-21 18:09:07 +08:00
{
2024-12-09 09:46:52 +08:00
instance = this;
//recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
2024-10-22 17:37:42 +08:00
anim = this.GetComponent<Animator>();
navMeshAgent = GetComponent<NavMeshAgent>();
// 初始化 NavMeshAgent 的一些属性
navMeshAgent.speed = 3.5f; // 设置速度
navMeshAgent.acceleration = 8f; // 设置加速度
navMeshAgent.angularSpeed = 120f; // 设置旋转速度
navMeshAgent.stoppingDistance = 1f; // 设置停止的距离
2024-10-21 18:09:07 +08:00
}
private void OnTriggerEnter(Collider other)
2024-10-21 18:09:07 +08:00
{
2024-10-24 18:01:55 +08:00
if(other.tag == "Player")
2024-10-21 18:09:07 +08:00
recusebtn.gameObject.SetActive(true);
2024-10-22 11:57:50 +08:00
if (statebool) return;
2024-10-24 12:46:20 +08:00
other.GetComponent<CharacterInturn>().cha = this.gameObject ;
2024-10-22 11:57:50 +08:00
}
2024-10-21 18:09:07 +08:00
2024-10-23 11:31:35 +08:00
private void OnTriggerExit(Collider other)
{
recusebtn.gameObject.SetActive(false);
}
2024-12-12 18:51:08 +08:00
public void SetNPCInfo(string id)
{
npcId = id;
}
//npc状态
public void Setnpcstate()
2024-10-22 17:37:42 +08:00
{
movebool = true;
nstate = Npcstate.idle;
2024-10-22 17:37:42 +08:00
recusebtn.gameObject.SetActive(false);
Debug.Log( npcId + "被救");
2024-12-09 09:46:52 +08:00
}
//添加npc的目的地到list中
2024-12-09 11:30:15 +08:00
public void SetNpcDes(Vector3 tar)
2024-12-09 09:46:52 +08:00
{
2024-12-12 14:50:53 +08:00
//target.position = tar;
targetPoints.Add(tar);
2024-10-22 17:37:42 +08:00
}
private void Update()
2024-10-22 11:57:50 +08:00
{
2024-12-13 19:17:57 +08:00
2024-12-13 19:11:14 +08:00
// 继续处理NPC的状态和动画
switch (nstate)
{
case Npcstate.idle:
SetAni(2);
break;
case Npcstate.run:
movebool = true;
Run(currentTarget);
break;
case Npcstate.dead:
SetAni(0);
break;
}
if (targetPoints.Count > 0) // 确保列表中有目标点
2024-10-22 17:37:42 +08:00
{
currentTarget = targetPoints[0]; // 获取当前的目标点
nstate = Npcstate.run;
movebool = true;
2024-12-13 19:17:57 +08:00
float dis = Vector3.Distance(this.transform.position, currentTarget);
// 判断是否到达目标点
2024-12-13 19:23:39 +08:00
if (movebool && dis < 0.1f)
2024-12-12 14:50:53 +08:00
{
Debug.Log("到达目标点");
2024-12-12 14:50:53 +08:00
// 设置NPC状态
nstate = Npcstate.idle;
movebool = false;
// 达到目标后,从列表中移除该目标点
if (targetPoints.Count > 1)
2024-12-12 14:50:53 +08:00
{
targetPoints = RemoveDes();
}
else
{
targetPoints.Clear(); // 清空列表
2024-12-12 14:50:53 +08:00
}
}
2024-10-22 17:37:42 +08:00
}
2024-12-13 19:11:14 +08:00
2024-11-29 15:13:32 +08:00
}
//移除第一个点
2024-12-12 14:50:53 +08:00
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)
2024-11-29 15:13:32 +08:00
{
if (movebool)
{
SetAni(1);
navMeshAgent.SetDestination(target); // 使用 NavMeshAgent 设置目标点
2024-11-29 15:13:32 +08:00
}
}
2024-10-22 17:37:42 +08:00
2024-11-29 15:13:32 +08:00
public void SetAni(int x)
{
anim.SetInteger("state", x);
2024-10-22 11:57:50 +08:00
}
2024-10-21 18:09:07 +08:00
2024-10-22 17:37:42 +08:00
2024-10-21 18:09:07 +08:00
}