using UnityEngine; using UnityEngine.AI; public class AIController : MonoBehaviour { private NavMeshAgent agent; private Transform player; void Start() { agent = GetComponent<NavMeshAgent>(); player = GameObject.FindGameObjectWithTag("Player").transform; } void Update() { // 检查是否可以到达玩家位置 NavMeshPath path = new NavMeshPath(); if (NavMesh.CalculatePath(transform.position, player.position, NavMesh.AllAreas, path)) { // 如果路径的状态是Complete,表示可以到达 if (path.status == NavMeshPathStatus.Complete) { agent.SetDestination(player.position); } else { // 无法到达,可以执行放弃追逐的逻辑 // 例如:停止追逐、切换到巡逻状态等 } } } } |
|
来自: 勤奋不止 > 《游戏引擎unity》