Failed to create agent because it is not close enough to the NavMesh

主要原因是:两个相同对象navmesh点太近造成。

解决方案:通过NavMesh.SamplePosition 获得可以行走点

for(int i = 0;i<100;i++)
        {
            float fRadius = Random.Range(0, mRadius);
            float fAngle = Random.Range(0, 3.14f);

            Vector3 v3 = mStartPos;
            v3.x += Mathf.Sin(fAngle) * fRadius;
            v3.z += Mathf.Cos(fAngle) * fRadius;

            NavMeshHit hit;
            if(NavMesh.SamplePosition(v3, out hit, mRadius, 1))
            {
                GameObject p = Instantiate<GameObject>(mCat, hit.position, Quaternion.identity, transform);
                if (p != null)
                {
                    NavMeshAgent agent = p.AddComponent<NavMeshAgent>();
                    if(agent!=null)
                    {
                        agent.speed = 5.0f;
                    }
                    mCatList.Add(p);
                }  
            }
            
        }

猜你喜欢

转载自www.cnblogs.com/ylwn817/p/10268176.html