首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unity的NavMesh代理在导航边的故障

Unity的NavMesh代理在导航边的故障
EN

Stack Overflow用户
提问于 2017-11-09 10:32:14
回答 1查看 1.7K关注 0票数 0

我最近开始玩人工智能和联合的内置的寻路。到目前为止,我还没有遇到什么更大的麻烦,但昨天我引进了一个山型模型,从它的底部到顶部有一条路。

我的人工智能游戏对象包括刚体(设置为运动学),胶囊对撞机和一个NavMesh代理。我生成了NavMesh,并将最大斜率设置为40度,当AI跟随我的玩家或沿着指定的路径(路径点)行走时,AI导航非常好,但是当我试图在AI半径内的NavMesh上选择一个随机位置时,它就会出故障,几秒钟后就开始摇晃,通常是在目的地接近NavMesh的边缘时。我拍摄了两段视频,从昨天开始我一直在努力解决这个问题。

我试过完全移除刚体,改变避障设置,降低最大坡度,但到目前为止没有任何效果。

如下所示:

视频1

视频2

我用来获取随机位置的代码:

巴斯丁

代码语言:javascript
复制
        void ControlRandomWander()
        {
            float pointDist = Vector3.Distance(currentWanderPos, transform.position);

            if(pointDist < 2f || currentWanderPos == Vector3.zero)
            {
                wanderWaitTimer += Time.deltaTime * 15;

                anims.LookAround(true);

                if (wanderWaitTimer >= wanderWaitTime)
                {
                    Vector3 randPos = GetRandomPositionAroundTarget(transform.position, -wanderRadius, wanderRadius);

                    NavMeshPath pth = new NavMeshPath();
                    NavMesh.CalculatePath(transform.position, randPos, agent.areaMask, pth);
                    float pathDist = 0f;

                    if (pth.status == NavMeshPathStatus.PathComplete)
                    {
                        for (int i = 0; i < pth.corners.Length - 1; i++)
                        {
                            pathDist += Vector3.Distance(pth.corners[i], pth.corners[i + 1]);
                        }

                        Debug.Log(pathDist);

                        if (pathDist <= wanderRadius)
                        {
                            currentWanderPos = randPos;

                            wanderWaitTime = Random.Range(wanderWaitMin, wanderWaitMax);

                            anims.LookAround(false);
                            wanderWaitTimer = 0;

                            MoveToPosition(randPos, true);
                        }
                    }
                }
            }
            else
            {
                if (agent.destination != currentWanderPos)
                {
                    MoveToPosition(currentWanderPos, true);
                }
            }
        }

        Vector3 GetRandomPositionAroundTarget(Vector3 pos, float minRange, float maxRange)
        {
            float offsetX = Random.Range(minRange, maxRange);
            float offsetZ = Random.Range(minRange, maxRange);

            Vector3 orgPos = pos;

            orgPos.x += offsetX;
            orgPos.z += offsetZ;

            NavMeshHit hit;
            if(NavMesh.SamplePosition(orgPos, out hit, 5f, agent.areaMask))
            {
                Debug.Log(hit.position);
                return hit.position;
            }

            Debug.Log(hit.position);

            return pos;
        }

我真的很感激你能帮我解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-09 12:04:30

事实证明,NavMesh.SamplePosition不考虑NavMesh代理半径,因此代理永远无法到达它。

我设法通过将hit.position移动到代理半径的一半远离边缘,从而修复了它。

代码语言:javascript
复制
if(NavMesh.SamplePosition(orgPos, out hit, 5f, agent.areaMask))
{
     Vector3 ret = hit.position;
     Vector3 pathDir = pos - ret;
     ret += pathDir.normalized * (agent.radius / 2);

     return ret;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47199536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档