只能放置第一个 NavMeshAgent

Only first NavMeshAgent can be placed

我遇到了只能创建第一个 NavMeshAgent 的问题。

首先,我为 NavMesh 烘焙对象选择了所有网格渲染器:

我对烘焙进行了微调,使其具有恰到好处的细节。如果我在烘焙代理选项中使用更大的半径,各种对象周围的黑色间隙会更大:

我在后斜坡的两侧放置了两个刷怪笼(里面有白色球体)。这些是不可见的,但在脚本中,NavMeshObjects 以一定间隔放置在这些位置上,并将其目的地设置为其他游戏对象之一。

第一个工作正常,但生成器队列中的其他 17 个 NavMeshObjects 中的 none 可以设置它们的目的地。

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,使用 Warp 设置 NavMeshAgent 的初始位置,并确保点在网格上。

问题与 NavMeshagent 无关。是这一行:

 minion.transform.parent = transform;

我需要将其更改为:

 new_minion.transform.parent = transform;

基本上 minion 是被克隆的对象,new_minion 是克隆对象。因此,如果我设置 minion 的父级,我想我会把它弄乱以备将来克隆。