_xiaofang/xiaofang/Assets/Script/npc/RecuseNpc.cs
2024-12-23 09:55:15 +08:00

206 lines
4.6 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;
public Button recusebtn;
private bool statebool = false;
private Animator anim;
public Npcstate nstate = Npcstate.idle;
private bool movebool = false;
public Transform target;
public Vector3 currentTarget;
// 存目的地的list
public List<Vector3> targetPoints = new List<Vector3>();
private void Awake()
{
instance = this;
//recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
anim = this.GetComponent<Animator>();
target = GameObject.Find("mubiao").GetComponent<Transform>();
navMeshAgent = GetComponent<NavMeshAgent>();
// 初始化 NavMeshAgent 的一些属性
navMeshAgent.speed = 2f; // 设置速度
navMeshAgent.acceleration = 8f; // 设置加速度
navMeshAgent.angularSpeed = 120f; // 设置旋转速度
navMeshAgent.stoppingDistance = 1f; // 设置停止的距离
}
private void OnTriggerEnter(Collider other)
{
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;
}
//npc状态
public void Setnpcstate()
{
movebool = true;
nstate = Npcstate.idle;
recusebtn.gameObject.SetActive(false);
Debug.Log(npcId + "被救");
}
//添加npc的目的地到list中
public void SetNpcDes(Vector3 tar)
{
Debug.Log(tar+"目标路径点");
target.position = tar;
Debug.Log(target);
NavMeshHit hit;
if (!NavMesh.SamplePosition(tar, out hit, 1.0f, NavMesh.AllAreas))
{
Debug.LogError($"目标点 {target} 不在导航网格上");
}
else
{
tar = hit.position; // 将目标点调整到最近的导航网格位置
}
Debug.Log("进入奔跑++++++=");
//targetPoints.Clear();
targetPoints.Add(tar);
}
private void Update()
{
// 继续处理NPC的状态和动画
switch (nstate)
{
case Npcstate.idle:
SetAni(2);
break;
case Npcstate.run:
movebool = true;
Run(currentTarget);
break;
case Npcstate.dead:
SetAni(0);
break;
}
Debug.Log("++++++++++++++++++++++++++++" + targetPoints.Count);
if (targetPoints.Count > 0 && currentTarget != null)
{
currentTarget = targetPoints[targetPoints.Count-1];
nstate = Npcstate.run;
movebool = true;
float dis = Vector3.Distance(transform.position, currentTarget);
// 停止条件
if (movebool && dis <= 0.25f)
{
Debug.Log("到达目标点");
nstate = Npcstate.idle;
movebool = false;
if (targetPoints.Count > 1)
{
targetPoints = RemoveDes();
}
else
{
targetPoints.Clear();
}
}
else
{
return;
}
}
}
//移除第一个点
public List<Vector3> RemoveDes()
{
List<Vector3> list = new List<Vector3>();
for (int i = 1; i < targetPoints.Count; i++)
{
list.Add(targetPoints[i]);
}
return list;
}
public void Run(Vector3 target)
{
if (!navMeshAgent.enabled)
{
Debug.LogError("NavMeshAgent 未启用!");
return;
}
if (navMeshAgent.isStopped)
{
navMeshAgent.isStopped = false; // 取消停止状态
}
Debug.Log("进入奔跑状态");
if (movebool)
{
NavMeshPath path = new NavMeshPath();
navMeshAgent.SetDestination(target);
SetAni(1); // 设置奔跑动画
}
}
public void SetAni(int x)
{
anim.SetInteger("state", x);
}
}