将npc移动改成navmesh的方法移动

This commit is contained in:
huyulong 2024-12-13 09:43:21 +08:00
parent 26e6f4e6d1
commit 630a853c21
3 changed files with 58 additions and 22 deletions

View File

@ -1485,7 +1485,30 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ea3c4e24ebc42194181651236cdf30f1, type: 3} m_Script: {fileID: 11500000, guid: ea3c4e24ebc42194181651236cdf30f1, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
npcId:
nstate: 1 nstate: 1
target: {fileID: 0} target: {fileID: 0}
currentTarget: {x: 0, y: 0, z: 0} currentTarget: {x: 0, y: 0, z: 0}
targetPoints: [] targetPoints: []
--- !u!195 &3307599082991733569
NavMeshAgent:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9144465012532105757}
m_Enabled: 1
m_AgentTypeID: 0
m_Radius: 0.5
m_Speed: 3.5
m_Acceleration: 8
avoidancePriority: 50
m_AngularSpeed: 120
m_StoppingDistance: 0
m_AutoTraverseOffMeshLink: 1
m_AutoBraking: 1
m_AutoRepath: 1
m_Height: 2
m_BaseOffset: 0
m_WalkableMask: 4294967295
m_ObstacleAvoidanceType: 4

View File

@ -27283,5 +27283,9 @@ PrefabInstance:
propertyPath: m_Name propertyPath: m_Name
value: Vang value: Vang
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9144465012532105757, guid: 4bc999dc543a61148a0ba3bb1dd45a4d, type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 4bc999dc543a61148a0ba3bb1dd45a4d, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 4bc999dc543a61148a0ba3bb1dd45a4d, type: 3}

View File

@ -2,6 +2,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using UnityEngine.AI;
public enum Npcstate public enum Npcstate
{ {
@ -12,6 +13,8 @@ public enum Npcstate
public class RecuseNpc : MonoBehaviour public class RecuseNpc : MonoBehaviour
{ {
private NavMeshAgent navMeshAgent;//navmesh组件
public string npcId; public string npcId;
public static RecuseNpc instance; public static RecuseNpc instance;
@ -29,7 +32,7 @@ public class RecuseNpc : MonoBehaviour
public Vector3 currentTarget; public Vector3 currentTarget;
// 存储目标点的List // <EFBFBD>洢Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>List
public List<Vector3> targetPoints = new List<Vector3>(); public List<Vector3> targetPoints = new List<Vector3>();
private void Awake() private void Awake()
@ -38,9 +41,17 @@ public class RecuseNpc : MonoBehaviour
recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>(); recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
anim = this.GetComponent<Animator>(); 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)//通过触发器得到对应的救援组人员标签是否显现出UI private void OnTriggerEnter(Collider other)//ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ľ<EFBFBD>Ԯ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD>ǩ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD>UI
{ {
if(other.tag == "Player") if(other.tag == "Player")
recusebtn.gameObject.SetActive(true); recusebtn.gameObject.SetActive(true);
@ -58,51 +69,51 @@ public class RecuseNpc : MonoBehaviour
npcId = id; npcId = id;
} }
//设置NPC的状态 //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>NPC<EFBFBD><EFBFBD>״̬
public void Setnpcstate()//点击救援按钮执行完动作后对按钮进行隐藏 public void Setnpcstate()//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԯ<EFBFBD><EFBFBD>ťִ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>԰<EFBFBD>ť<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{ {
movebool = true; movebool = true;
nstate = Npcstate.run; nstate = Npcstate.run;
recusebtn.gameObject.SetActive(false); recusebtn.gameObject.SetActive(false);
Debug.Log("Setnpcstate调用"); Debug.Log("Setnpcstate<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
} }
//设置NPC的目标点 //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>NPC<EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD>
public void SetNpcDes(Vector3 tar) public void SetNpcDes(Vector3 tar)
{ {
//target.position = tar; //target.position = tar;
targetPoints.Add(tar); // 将目标点添加到列表中 targetPoints.Add(tar); // <EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
} }
private void Update() private void Update()
{ {
if (targetPoints.Count > 0) // 确保列表中有目标点 if (targetPoints.Count > 0) // 确保列表中有目标点
{ {
currentTarget = targetPoints[0]; // 获取当前的目标点 currentTarget = targetPoints[0]; // 获取当前的目标点
if (Vector3.Distance(transform.position, currentTarget) < 1.3f && movebool && currentTarget != null) // 判断人物被救援后移动到目标位置 // 判断是否到达目标点
if (movebool && navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
{ {
Debug.Log("到达目标点"); Debug.Log("到达目标点");
// 达到目标后,从列表中移除该目标点 // 达到目标后,从列表中移除该目标点
if(targetPoints.Count > 1) if (targetPoints.Count > 1)
{ {
targetPoints = RemoveDes(); targetPoints = RemoveDes();
} }
else else
{ {
targetPoints = null; targetPoints.Clear(); // 清空列表
} }
// 设置NPC状态
// 设置NPC状态
nstate = Npcstate.idle; nstate = Npcstate.idle;
movebool = false; movebool = false;
} }
} }
// 继续处理NPC的状态和动画 // 继续处理NPC的状态和动画
switch (nstate) switch (nstate)
{ {
case Npcstate.idle: case Npcstate.idle:
@ -118,7 +129,7 @@ public class RecuseNpc : MonoBehaviour
} }
} }
//删除目标 //移除第一个
public List<Vector3> RemoveDes() public List<Vector3> RemoveDes()
{ {
List<Vector3> list = new List<Vector3>(); List<Vector3> list = new List<Vector3>();
@ -132,14 +143,12 @@ public class RecuseNpc : MonoBehaviour
} }
//跑步逻辑
public void Run(Vector3 target) public void Run(Vector3 target)
{ {
if (movebool) if (movebool)
{ {
SetAni(1); SetAni(1);
transform.LookAt(target); navMeshAgent.SetDestination(target); // 使用 NavMeshAgent 设置目标点
transform.position = Vector3.Lerp(transform.position, target, 0.3f * Time.deltaTime);
} }
} }