Unity-告訴導航網(wǎng)格代理移動到目標位置
2021-03-12 10:11 作者:unity_某某師_高錦錦 | 我要投稿
只需將?NavMeshAgent.destination?屬性設置為您希望代理移動到的點,即可告訴代理開始計算路徑。計算完成后,代理將自動沿路徑移動,直至到達目標位置。下面的代碼實現(xiàn)了一個簡單的類,該類使用一個游戲對象來標記在?Start?函數(shù)中分配給?destination?屬性的目標點。請注意,該腳本假定您已從 Editor 中添加并配置了導航網(wǎng)格代理 (NavMeshAgent) 組件。
? ?// MoveDestination.cs
? ? ? ?
using UnityEngine;
? ?
? ? ? ?
public class MoveDestination : MonoBehaviour {
? ? ?
? ? ? ? ??
public Transform goal;
? ? ?
? ? ? ? ??
void Start () {
? ? ? ? ? ? ?
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.destination = goal.position;
? ? ? ? ??
}
? ? ? ?
}
? ?
// MoveDestination.js
? ? ? ?
var goal: Transform;
? ? ? ?
function Start() {
? ? ? ? ?
var agent: NavMeshAgent = GetComponent.<NavMeshAgent>();
agent.destination = goal.position;
}
標簽:
Unity-告訴導航網(wǎng)格代理移動到目標位置的評論 (共 條)
分享到微博請遵守國家法律