我遇到了只能创建第一个 NavMeshAgent 的问题。
首先,我为 NavMesh 烘焙对象选择了所有网格渲染器:
我对烘焙进行了微调,使其具有恰到好处的细节。如果我在烘焙代理选项中使用更大的半径,则各种对象周围的黑色间隙会更大:
我在后坡道的任一侧放置了两个生成器(其中有白色球体的那个)。这些是不可见的,但在脚本中,NavMeshObjects 以一定的间隔放置在这些位置上,并将其目的地设置为其他游戏对象之一。
第一个工作正常,但生成器队列中的其他 17 个 NavMeshObject 都不能设置其目的地。
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
,并确保这些点位于网格上。