iLMS知識社群歷程檔登入
位置: 黃國哲 > Unity
1100912 導航
by 黃國哲 2021-09-12 10:15:33, 回應(0), 人氣(446)




場景為設成static

主角加入元件nav mesh agent

按下bake可以顯示可移動的範圍

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; //呼叫AI引擎

public class Navil1 : MonoBehaviour {

public Transform Target1; //宣告目標
NavMeshAgent Agent1 ;  //宣告導航代理器

void Start () {
Agent1 = GetComponent<NavMeshAgent>(); //啟動導航
}

void Update(){
if(Input.GetKey(KeyCode.S))
{
Agent1.SetDestination(Target1.position); //設定導航目標
}
}
}
回應