NavMesh.SamplePosition always returns false? Why?

I suspect it has to be a problem with the area mask bit of sampleposition. If I set it to 1 or ~Navmesh.GetAreaByName(“Walkable”) the “Rats” will spawn as I intend. What’s wrong with my sampleposition? Does getareabyname return the wrong bit or something?

The area mask on the rats navmeshagent is also set to “Walkable”

            if (NavMesh.SamplePosition(transform.position, out hit, 2f, NavMesh.GetAreaByName("Walkable")))
{
                GameObject rat = Instantiate(rats, hit.position, Quaternion.identity);

Hi @Razputin , please make sure to use correct code tags when posting your code snippets to make it more readable.

Navmesh.GetAreaByName("areaName")

will return -1 if the area was not found. You could check what is the return value of that method for your area name to make sure that it’s correct.

The area returned is 0 which should be “Walkable” I think, which is why I don’t understand why I can’t find the navmesh.

If I manually put “1” as the area mask it seems to properly adhere to it’s NavMeshSurface… why does 1 appear to work?

Sorry, I noticed it now. It’s because you’re passing an area index instead of the area mask. Can you try this?

if(NavMesh.SamplePosition(transform.position, out var hit, 2f, 1 << NavMesh.GetAreaFromName("Walkable")))
1 Like

Thank you, it was the bitshifting, I had tried this earlier and it wasn’t working but I believe I probably had my navmeshsurface wrong as well at that time. It’s working good now, thank you!

2 Likes