寻路逻辑,给每个npc一个目标点

This commit is contained in:
huyulong 2024-12-14 15:01:16 +08:00
parent aea6ce1bfa
commit 52ba7a2567
2 changed files with 68 additions and 31 deletions

View File

@ -1,5 +1,36 @@
%YAML 1.1 %YAML 1.1
%TAG !u! tag:unity3d.com,2011: %TAG !u! tag:unity3d.com,2011:
--- !u!1 &4995935176814719679
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4972410492480051969}
m_Layer: 0
m_Name: mubiao
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &4972410492480051969
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4995935176814719679}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 8479287258615548071}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &3353283690660036973 --- !u!1001 &3353283690660036973
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1441,6 +1472,11 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 2c4a835a9676e1a478c77d64f472c3ec, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 2c4a835a9676e1a478c77d64f472c3ec, type: 3}
--- !u!4 &8479287258615548071 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 6567720597980285386, guid: 2c4a835a9676e1a478c77d64f472c3ec, type: 3}
m_PrefabInstance: {fileID: 3353283690660036973}
m_PrefabAsset: {fileID: 0}
--- !u!1 &9144465012532105757 stripped --- !u!1 &9144465012532105757 stripped
GameObject: GameObject:
m_CorrespondingSourceObject: {fileID: 5795828957522809712, guid: 2c4a835a9676e1a478c77d64f472c3ec, type: 3} m_CorrespondingSourceObject: {fileID: 5795828957522809712, guid: 2c4a835a9676e1a478c77d64f472c3ec, type: 3}
@ -1473,23 +1509,6 @@ BoxCollider:
serializedVersion: 2 serializedVersion: 2
m_Size: {x: 0.4576242, y: 1.53, z: 0.77643585} m_Size: {x: 0.4576242, y: 1.53, z: 0.77643585}
m_Center: {x: -0.013215661, y: 0.77, z: 0.009899139} m_Center: {x: -0.013215661, y: 0.77, z: 0.009899139}
--- !u!114 &5519024520137767209
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9144465012532105757}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ea3c4e24ebc42194181651236cdf30f1, type: 3}
m_Name:
m_EditorClassIdentifier:
npcId:
nstate: 1
target: {fileID: 0}
currentTarget: {x: 0, y: 0, z: 0}
targetPoints: []
--- !u!195 &3307599082991733569 --- !u!195 &3307599082991733569
NavMeshAgent: NavMeshAgent:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1512,3 +1531,21 @@ NavMeshAgent:
m_BaseOffset: 0 m_BaseOffset: 0
m_WalkableMask: 4294967295 m_WalkableMask: 4294967295
m_ObstacleAvoidanceType: 4 m_ObstacleAvoidanceType: 4
--- !u!114 &5519024520137767209
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9144465012532105757}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ea3c4e24ebc42194181651236cdf30f1, type: 3}
m_Name:
m_EditorClassIdentifier:
npcId:
recusebtn: {fileID: 0}
nstate: 1
target: {fileID: 0}
currentTarget: {x: 0, y: 0, z: 0}
targetPoints: []

View File

@ -5,10 +5,10 @@ using UnityEngine.UI;
using UnityEngine.AI; using UnityEngine.AI;
public enum Npcstate public enum Npcstate
{ {
idle, idle,
dead, dead,
run run
} }
public class RecuseNpc : MonoBehaviour public class RecuseNpc : MonoBehaviour
@ -45,7 +45,7 @@ public class RecuseNpc : MonoBehaviour
instance = this; instance = this;
//recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>(); //recusebtn = GameObject.Find("Canvas/Recuse").GetComponent<Button>();
anim = this.GetComponent<Animator>(); anim = this.GetComponent<Animator>();
target = transform.Find("mubiao").GetComponent<Transform>();
navMeshAgent = GetComponent<NavMeshAgent>(); navMeshAgent = GetComponent<NavMeshAgent>();
// 初始化 NavMeshAgent 的一些属性 // 初始化 NavMeshAgent 的一些属性
@ -58,10 +58,10 @@ public class RecuseNpc : MonoBehaviour
private void OnTriggerEnter(Collider other) private void OnTriggerEnter(Collider other)
{ {
if(other.tag == "Player") if (other.tag == "Player")
recusebtn.gameObject.SetActive(true); recusebtn.gameObject.SetActive(true);
if (statebool) return; if (statebool) return;
other.GetComponent<CharacterInturn>().cha = this.gameObject ; other.GetComponent<CharacterInturn>().cha = this.gameObject;
} }
private void OnTriggerExit(Collider other) private void OnTriggerExit(Collider other)
@ -76,18 +76,18 @@ public class RecuseNpc : MonoBehaviour
//npc状态 //npc状态
public void Setnpcstate() public void Setnpcstate()
{ {
movebool = true; movebool = true;
nstate = Npcstate.idle; nstate = Npcstate.idle;
recusebtn.gameObject.SetActive(false); recusebtn.gameObject.SetActive(false);
Debug.Log( npcId + "被救"); Debug.Log(npcId + "被救");
} }
//添加npc的目的地到list中 //添加npc的目的地到list中
public void SetNpcDes(Vector3 tar) public void SetNpcDes(Vector3 tar)
{ {
Debug.Log(tar); Debug.Log(tar);
//target.position = tar; target.position = tar;
NavMeshHit hit; NavMeshHit hit;
if (!NavMesh.SamplePosition(tar, out hit, 1.0f, NavMesh.AllAreas)) if (!NavMesh.SamplePosition(tar, out hit, 1.0f, NavMesh.AllAreas))
{ {
@ -103,6 +103,7 @@ public class RecuseNpc : MonoBehaviour
private void Update() private void Update()
{ {
if (targetPoints.Count > 0) if (targetPoints.Count > 0)
{ {
currentTarget = targetPoints[0]; currentTarget = targetPoints[0];
@ -111,8 +112,8 @@ public class RecuseNpc : MonoBehaviour
float dis = Vector3.Distance(transform.position, currentTarget); float dis = Vector3.Distance(transform.position, currentTarget);
// 停止条件改为 NavMeshAgent.stoppingDistance // 停止条件改为
if (movebool && dis <= 0.2f) if (movebool && dis <= 0.1f)
{ {
Debug.Log("到达目标点"); Debug.Log("到达目标点");
nstate = Npcstate.idle; nstate = Npcstate.idle;
@ -150,7 +151,6 @@ public class RecuseNpc : MonoBehaviour
public void Run(Vector3 target) public void Run(Vector3 target)
{ {
Debug.Log(target);
if (movebool) if (movebool)
{ {
// 确保目标点在 NavMesh 上 // 确保目标点在 NavMesh 上