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

195 lines
4.3 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,
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-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;
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-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;
//recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
2024-10-22 17:37:42 +08:00
anim = this.GetComponent<Animator>();
target = transform.Find("mubiao").GetComponent<Transform>();
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
{
if (other.tag == "Player")
recusebtn.gameObject.SetActive(true);
2024-10-22 11:57:50 +08:00
if (statebool) return;
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-14 14:31:48 +08:00
Debug.Log(tar);
target.position = tar;
2024-12-13 19:33:27 +08:00
NavMeshHit hit;
if (!NavMesh.SamplePosition(tar, out hit, 1.0f, NavMesh.AllAreas))
{
Debug.LogError($"目标点 {target} 不在导航网格上");
}
else
{
tar = hit.position; // 将目标点调整到最近的导航网格位置
}
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 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-13 19:29:43 +08:00
if (targetPoints.Count > 0)
2024-12-13 19:11:14 +08:00
{
2024-12-13 19:29:43 +08:00
currentTarget = targetPoints[0];
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-14 15:19:22 +08:00
// 停止条件
if (movebool && dis <= 0.1f)
2024-12-13 19:29:43 +08:00
{
Debug.Log("到达目标点");
nstate = Npcstate.idle;
movebool = false;
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-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
{
if (movebool)
{
2024-12-13 19:29:43 +08:00
// 确保目标点在 NavMesh 上
NavMeshHit hit;
if (NavMesh.SamplePosition(target, out hit, 1.0f, NavMesh.AllAreas))
{
navMeshAgent.SetDestination(hit.position);
SetAni(1);
}
else
{
Debug.LogError($"目标点 {target} 不在导航网格上");
}
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
}