只能放置第一个NavMeshAgent

时间:2017-10-04 06:40:28

标签: c# unity3d navmesh

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

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

enter image description here

我稍微调整了烘焙次数,因此它有足够的细节。如果我在烘焙代理选项中使用较大的半径,则各种对象周围的黑色间隙会更大:

enter image description here

我有两个产卵器放置在后坡道的两侧(其中有一个白色球体)。这些是不可见的,但在脚本中,NavMeshObjects按间隔放置在这些位置上,并将其目标设置为其他游戏对象之一。

第一个工作正常,但产生器队列中的其他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,使用Warp设置NavMeshAgent的初始位置,并确保点在网格上。

1 个答案:

答案 0 :(得分:0)

这个问题与NavMeshagent无关。就是这条线:

 minion.transform.parent = transform;

我需要将其更改为:

 new_minion.transform.parent = transform;

基本上minion是克隆的对象,new_minion是克隆。因此,如果我设置了minion的父级,我猜它将为未来的克隆搞砸了。