|
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() { // 檢查是否可以到達(dá)玩家位置 NavMeshPath path = new NavMeshPath(); if (NavMesh.CalculatePath(transform.position, player.position, NavMesh.AllAreas, path)) { // 如果路徑的狀態(tài)是Complete,表示可以到達(dá) if (path.status == NavMeshPathStatus.Complete) { agent.SetDestination(player.position); } else { // 無(wú)法到達(dá),可以執(zhí)行放棄追逐的邏輯 // 例如:停止追逐、切換到巡邏狀態(tài)等 } } } } |
|
|
來(lái)自: 勤奮不止 > 《游戲引擎unity》