我遇到了只能創建第一個NavMeshAgent的問題。只有第一個NavMeshAgent可以放置
首先,我選擇了導航網格烘對象的所有網格渲染:
我微調烘烤了一點,所以它剛剛細節適量。
我有兩個產卵魚放在後面斜坡(具有白色球體的任一側:如果我用在焙烤代理選項更大的半徑的各種對象周圍的黑間隙將更大在裏面)。這些是不可見的,但在腳本中,NavMeshObjects按間隔放置在這些位置上,並將其目標設置爲其他遊戲對象之一。
第一個工作正常,但spawner隊列中的其他17個NavMeshObjects都沒有設置其目的地。
public class MinionSpawner : MonoBehaviour {
public void spawn (GameObject minion, GameObject target_position_obj) {
// Find the target position from the passed gameobject
MinionTargetPosition target_position = target_position_obj.GetComponent<MinionTargetPosition>();
if (!target_position) { throw new System.Exception("no valid target position given"); }
// Instantiate the given minion at the spawner's origin point
GameObject new_minion = Object.Instantiate(minion, transform.position, new Quaternion(0,0,0,0));
new_minion.SetActive(true);
// Mark the minion at it's target position, for lookup upon collision or elsewhere
MinionAttributes minion_attrs = new_minion.GetComponentInChildren<MinionAttributes>(true);
if (!minion_attrs) { throw new System.Exception("object passed as minion does not have MinionAttributes"); }
minion_attrs.target_position = target_position;
// Configure a NavMeshAgent to navigate the minion from origin to target position.
NavMeshAgent nav_mesh_agent = minion_attrs.gameObject.GetComponent<NavMeshAgent>();
if (!nav_mesh_agent) { throw new System.Exception("minion doesn't have a nav mesh agent"); };
nav_mesh_agent.Warp(transform.position);
// ================================================================
// THIS LINE FAILS:
nav_mesh_agent.destination = target_position.transform.position;
// ================================================================
// mark the minion's position as occupied in the turret's dictionary
Turret turret = target_position.turret;
turret.minion_slots[target_position] = true;
// set the minion's parent to the minion spawner, for organization's sake.
minion.transform.parent = transform;
}
}
我得到錯誤"SetDestination" can only be called on an active agent that has been placed on a NavMesh.
,我知道有關於此的問題。但我已經嘗試了我發現的所有建議,包括烘焙NavMesh,將NavMeshAgent的初始位置設置爲Warp
,並確保點位於網格上。