//思路:自動尋路是根據場景中NavMeshAgent的功能,來自於AI的引用集。利用渲染以后的目標的位置設置來跟蹤目標的位置
//注意點:此方法中舊的stop和resume方法已經棄用了
代碼:
// 1.設置追蹤點
private NavMeshAgent agent;
private Transform player; //2.設置目標點
void Awake(){
agent=this.GetComponent<NavMeshAgent>(); //3.初始化追蹤點
}
void Start(){
player=GameObject.FindGameObjectWithTag("Player").transform; //4.初始化目標點
}
void Updata(){
if(Vector3.Distance(transform.position,palyer.position)<3f){ //6.判斷如果目標點的位置小魚2f的時候
agent.isStop=true; //6.1追蹤點停止追蹤
}else{
agent,isStop=false; //6.2追蹤點開始追蹤
agent.SetDestination(palyer.position); //5.設置追蹤點的目標位置
}
}