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》