【问题标题】:Only first NavMeshAgent can be placed只能放置第一个 NavMeshAgent
【发布时间】:2017-10-04 06:40:27
【问题描述】:

我遇到了只能创建第一个 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,使用Warp 设置 NavMeshAgent 的初始位置,并确保这些点位于网格上。

【问题讨论】:

    标签: c# unity3d navmesh


    【解决方案1】:

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

     minion.transform.parent = transform;
    

    我需要将其更改为:

     new_minion.transform.parent = transform;
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      相关资源
      最近更新 更多