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

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

With multi-dimensional model as the core, let the factory digital transformation and upgrading “within reach”>>>

The main reason is that the navmesh points of two identical objects are too close

Solution: get walkable points through 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);
                }  
            }
            
        }