So today I was refurbishing a scene when this happened. After baking a new Navmesh the SamplePosition() returned only infinity. I tried a lot of things out but eventually went back to an older version of my project. Sooner or later I will have to bake a new Navmesh though, so I'd really like to know what happened there.

Here is the part of the code that appears to cause trouble:

Vector3 randomDirection = Random.insideUnitSphere * roamRadius;
NavMeshHit hit;
NavMesh.SamplePosition(randomDirection, out hit, roamRadius, 1);
direction = hit.position;

And of course this, since it tries to look into infinity I suppose:

transform.LookAt(transform.position - direction, Vector3.up);

Again, this code works fine. Only after baking a Navmesh this fails horribly.


Better later than never with an answer: 5.3.5 here, I had the same issue.
The solution was to "bake" (got the joke?) the last parameter for SamplePosition() to "1". Everything else fails.
I tried:
NavMesh.GetAreaFromName([anything]) FAIL
NavMesh.AllAreas FAIL
1 << NavMesh.GetNavMeshLayerFromName("Default") (from an old post, also tried with "Walkable" and without shifting) FAIL, etc.

The only thing worked was this:
NavMesh.SamplePosition(transform.position, out nHit, 0.1f, 1);
(The distance parameter -0.1f in the example- can be anything, that param actually works...).

Hope this helps! If too late for an answer, well, hope it saves a few hours for a random visitor! :)


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.