微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NavMeshAgent nextPosition 和目的地更改启用

如何解决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/的其他人找到答案

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。