2024-10-21 18:09:07 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
2024-12-13 09:43:21 +08:00
|
|
|
using UnityEngine.AI;
|
2024-10-22 11:57:50 +08:00
|
|
|
|
2024-10-22 17:37:42 +08:00
|
|
|
public enum Npcstate
|
2024-12-14 15:01:16 +08:00
|
|
|
{
|
|
|
|
idle,
|
|
|
|
dead,
|
|
|
|
run
|
2024-10-22 11:57:50 +08:00
|
|
|
}
|
|
|
|
|
2024-10-21 18:09:07 +08:00
|
|
|
public class RecuseNpc : MonoBehaviour
|
|
|
|
{
|
2024-12-13 17:19:27 +08:00
|
|
|
|
|
|
|
|
2024-12-13 09:43:21 +08:00
|
|
|
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;
|
|
|
|
|
2024-12-13 17:19:27 +08:00
|
|
|
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-12-13 19:40:29 +08:00
|
|
|
public Npcstate nstate = Npcstate.idle;
|
2024-10-22 11:57:50 +08:00
|
|
|
|
2024-10-22 17:37:42 +08:00
|
|
|
private bool movebool = false;
|
2024-11-30 16:52:33 +08:00
|
|
|
public Transform target;
|
2024-12-12 14:50:53 +08:00
|
|
|
|
|
|
|
public Vector3 currentTarget;
|
|
|
|
|
2024-12-13 17:19:27 +08:00
|
|
|
// 存目的地的list
|
2024-12-12 14:50:53 +08:00
|
|
|
public List<Vector3> targetPoints = new List<Vector3>();
|
|
|
|
|
2024-12-13 19:33:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2024-12-13 17:19:27 +08:00
|
|
|
//recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
|
2024-10-22 17:37:42 +08:00
|
|
|
anim = this.GetComponent<Animator>();
|
2024-12-14 15:43:47 +08:00
|
|
|
target = GameObject.Find("mubiao").GetComponent<Transform>();
|
2024-12-13 09:43:21 +08:00
|
|
|
navMeshAgent = GetComponent<NavMeshAgent>();
|
|
|
|
|
|
|
|
// 初始化 NavMeshAgent 的一些属性
|
2024-12-23 09:55:15 +08:00
|
|
|
navMeshAgent.speed = 2f; // 设置速度
|
2024-12-13 09:43:21 +08:00
|
|
|
navMeshAgent.acceleration = 8f; // 设置加速度
|
|
|
|
navMeshAgent.angularSpeed = 120f; // 设置旋转速度
|
|
|
|
navMeshAgent.stoppingDistance = 1f; // 设置停止的距离
|
|
|
|
|
2024-10-21 18:09:07 +08:00
|
|
|
}
|
|
|
|
|
2024-12-13 17:19:27 +08:00
|
|
|
private void OnTriggerEnter(Collider other)
|
2024-10-21 18:09:07 +08:00
|
|
|
{
|
2024-12-14 15:01:16 +08:00
|
|
|
if (other.tag == "Player")
|
|
|
|
recusebtn.gameObject.SetActive(true);
|
2024-10-22 11:57:50 +08:00
|
|
|
if (statebool) return;
|
2024-12-14 16:33:31 +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;
|
|
|
|
}
|
|
|
|
|
2024-12-13 17:19:27 +08:00
|
|
|
//npc状态
|
|
|
|
public void Setnpcstate()
|
2024-12-14 15:01:16 +08:00
|
|
|
{
|
2024-10-22 17:37:42 +08:00
|
|
|
movebool = true;
|
2024-12-13 17:19:27 +08:00
|
|
|
nstate = Npcstate.idle;
|
2024-10-22 17:37:42 +08:00
|
|
|
recusebtn.gameObject.SetActive(false);
|
2024-12-14 15:01:16 +08:00
|
|
|
Debug.Log(npcId + "被救");
|
2024-12-09 09:46:52 +08:00
|
|
|
}
|
|
|
|
|
2024-12-13 17:19:27 +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-16 11:58:13 +08:00
|
|
|
Debug.Log(tar+"目标路径点");
|
|
|
|
|
2024-12-16 12:01:51 +08:00
|
|
|
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("进入奔跑++++++=");
|
2024-12-16 14:14:25 +08:00
|
|
|
//targetPoints.Clear();
|
2024-12-16 12:01:51 +08:00
|
|
|
targetPoints.Add(tar);
|
|
|
|
|
2024-12-13 19:33:27 +08:00
|
|
|
|
2024-10-22 17:37:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Update()
|
2024-10-22 11:57:50 +08:00
|
|
|
{
|
2024-12-14 16:33:31 +08:00
|
|
|
|
2024-12-14 15:19:22 +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;
|
|
|
|
}
|
2024-12-14 16:33:31 +08:00
|
|
|
|
2024-12-14 15:43:47 +08:00
|
|
|
Debug.Log("++++++++++++++++++++++++++++" + targetPoints.Count);
|
2024-12-14 16:01:31 +08:00
|
|
|
if (targetPoints.Count > 0 && currentTarget != null)
|
2024-12-13 19:11:14 +08:00
|
|
|
{
|
2024-12-14 15:43:47 +08:00
|
|
|
|
2024-12-16 14:14:25 +08:00
|
|
|
currentTarget = targetPoints[targetPoints.Count-1];
|
2024-12-13 17:19:27 +08:00
|
|
|
nstate = Npcstate.run;
|
|
|
|
movebool = true;
|
2024-12-12 14:50:53 +08:00
|
|
|
|
2024-12-13 19:29:43 +08:00
|
|
|
float dis = Vector3.Distance(transform.position, currentTarget);
|
2024-12-13 17:19:27 +08:00
|
|
|
|
2024-12-14 15:19:22 +08:00
|
|
|
// 停止条件
|
2024-12-16 15:27:24 +08:00
|
|
|
if (movebool && dis <= 0.25f)
|
2024-12-13 19:29:43 +08:00
|
|
|
{
|
|
|
|
Debug.Log("到达目标点");
|
2024-12-13 17:19:27 +08:00
|
|
|
nstate = Npcstate.idle;
|
|
|
|
movebool = false;
|
|
|
|
|
2024-12-13 09:43:21 +08:00
|
|
|
if (targetPoints.Count > 1)
|
2024-12-12 14:50:53 +08:00
|
|
|
{
|
|
|
|
targetPoints = RemoveDes();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-12-13 19:29:43 +08:00
|
|
|
targetPoints.Clear();
|
2024-12-12 14:50:53 +08:00
|
|
|
}
|
|
|
|
}
|
2024-12-13 19:31:29 +08:00
|
|
|
else
|
|
|
|
{
|
2024-12-14 15:19:22 +08:00
|
|
|
|
2024-12-13 19:31:29 +08:00
|
|
|
return;
|
|
|
|
}
|
2024-10-22 17:37:42 +08:00
|
|
|
}
|
2024-11-29 15:13:32 +08:00
|
|
|
}
|
|
|
|
|
2024-12-13 09:43:21 +08:00
|
|
|
//移除第一个点
|
2024-12-12 14:50:53 +08:00
|
|
|
public List<Vector3> RemoveDes()
|
|
|
|
{
|
|
|
|
List<Vector3> list = new List<Vector3>();
|
|
|
|
|
2024-12-13 19:27:04 +08:00
|
|
|
for (int i = 1; i < targetPoints.Count; i++)
|
2024-12-12 14:50:53 +08:00
|
|
|
{
|
2024-12-13 19:27:04 +08:00
|
|
|
list.Add(targetPoints[i]);
|
2024-12-12 14:50:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Run(Vector3 target)
|
2024-11-29 15:13:32 +08:00
|
|
|
{
|
2024-12-14 16:33:31 +08:00
|
|
|
if (!navMeshAgent.enabled)
|
|
|
|
{
|
|
|
|
Debug.LogError("NavMeshAgent 未启用!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-14 16:01:31 +08:00
|
|
|
if (navMeshAgent.isStopped)
|
|
|
|
{
|
|
|
|
navMeshAgent.isStopped = false; // 取消停止状态
|
|
|
|
}
|
2024-12-14 16:33:31 +08:00
|
|
|
|
|
|
|
Debug.Log("进入奔跑状态");
|
2024-11-29 15:13:32 +08:00
|
|
|
if (movebool)
|
|
|
|
{
|
2024-12-14 16:33:31 +08:00
|
|
|
NavMeshPath path = new NavMeshPath();
|
|
|
|
navMeshAgent.SetDestination(target);
|
|
|
|
|
|
|
|
SetAni(1); // 设置奔跑动画
|
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
|
|
|
}
|