NavMeshAgent nextPosition 和目的地更改启用

问题描述

我有一个回合制游戏,可以在 NavMeshAgents(选中时)和 NavMeshObjects(未选中时)单位之间切换

我只是使用 NavMeshAgent 来导航路径,然后我手动导航,因此我将 updatePosition 和 updateRotation 永久设置为关闭

当我选择代理时,如果我在启用代理之前检查代理的 nextPosition 和目的地,它们都与我的游戏对象相同,但是一旦启用代理,nextPos 和 dest 都会在 z 上跳跃约 2轴。

奇怪的是,我无法通过直接设置或通过 SetDestination 或 Warp 函数手动将 nextPosition 或目的地更改为正确的值。所以我最终得到了从错误位置开始的导航路径。

下面的代码带有描述我所看到内容的注释,我目前使用的是统一版本 2020.2.7f1,但已经尝试了一些 2.x 版本

    protected void SetCharacterNavAgentOn()
    {
        obstacle.enabled = false; // nextposition & destination are correct at this point
        agent.enabled = true; // but Now they have been offset
        agent.nextPosition = gameObject.GetComponent<Rigidbody>().position; // this doesn't change the nextPosition
        agent.SetDestination(gameObject.GetComponent<Rigidbody>().position); // this doesn't change the destination
        bool isSuccessfull = agent.Warp(gameObject.GetComponent<Rigidbody>().position); // this doesn't change the nextPos or destination despite returning true
        gameObject.GetComponent<BoxCollider>().enabled = false;
        gameObject.GetComponent<Rigidbody>().isKinematic = true;
    }

任何帮助都将不胜感激,可能是一个错误,但我已经四处挖掘并且无法找到其他人遇到相同问题的任何示例。

解决方法

在此主题中为正在寻找https://forum.unity.com/threads/disabling-navmesh-obstacle-and-enabling-navmesh-agent-on-unit-causes-jumping-before-moving.1010998/的其他人找到答案

基本上这是一个计时的事情,在启用代理之前需要时间重新烘焙网格。