Basically, I run SamplePosition with a radius of 2 to find a position. That returns a true, and I store the navmesh hit. position, which is on the navmesh. Then I run Sample Position again for the returned position with a radius of 1 - and it fails to find any nav position. That should never happen, right?
It seems like it happens when there’s a pretty big delta between the Navmesh and the Height Mesh (which I definitely have enabled) - but it also seems to be happening when there’s no visible navmesh or heightmesh, for instance inside a low object or NavMeshObstacle with Carve set, where the should be no navmesh at all.
Am I missing something important about this? Does SamplePosition assume you’re calling it only on a location near the height mesh or something? Is there any way to query for the height mesh instead of the normal navmesh?
This is the output of a script that I used to find navmesh holes. It just raycasts down, then runs sample position twice - once with radius 2, then radius 1. The green lines are areas where the second sample position passed, and the yellow are where it failed.
Here’s a look at the nav and height mesh - it’s got a bigger delta on the top where those yellow lines are in the previous image. (please ignore all the navmesh links)
I’m a little confused why there’s a height mesh underneath the object at all - I can’t see any navmesh in there, but that we’re seeing yellow lines under there at all seems to indicate there’s invisible navmesh there, even if I add an obstacle and carve it out (which shouldn’t be necessary since the height of the object is lower than my agent height)
Unity 2020.3.14f1 if that matters.
I’ve never had the problem you’re describing, but based on the images you have, it seems like you have multiple NavMeshes baked. If you have multiple NavMeshes baked and you are using NavMesh.SamplePosition with the “all areas” filter, I would not be surprised to see the point you have selected is returned. It appears there is a second NavMesh that is pathable below where you are trying to get a result for that may intersect with that point.
If you do not have multiple NavMeshes baked, I think you need to work on your input to the bake because this NavMesh is very strange looking and does not match your visible geometry
Good idea, but I definitely only have one navmesh baked. The blue is the navmesh, the pink is the heightmesh, which is not something that SamplePosition can access, as far as I know.
Agreed that the blue navmesh shape is very strange, but I have no control over the optimization algorithm that’s causing it to get all wonky like that. I’ve tried increasing the Voxel resolution for navmesh scanning, and it did make the navmesh match a little better, but it didn’t affect this SamplePos issue.
Thanks for your input!
It’s hard to say what else may be going in there, but I would recommend simplifying that area or excluding the objects from the bake that are causing the weird generation to see if that resolves the problem. The first time I didn’t see some of that was a NavMeshLink. I would also be interested to hear if you get the same results with the NavMeshLinks removed.
Navmesh links can’t be queried by SamplePosition, right? So there’s no reason why having those on would have any bearing on this. And the mesh is really low poly so I don’t understand why the navmesh can’t handle it.
i should mention this issue shows up infrequently in other parts of my levels too, so it’s not exactly feasible for me to just change the geo. There must be something fundamental about SamplePosition that either I don’t understand or is wrong here.
Edit: I have verified that removing the navmesh links doesn’t affect this issue. I also confirmed that removing the rotation of the orange-lined object does not fix it either, though it did make the navmesh on top of the object slightly more sensible. That said, if you look at it from under the arch, it actually looks fine, it’s just clipping under the arch a little. And the heightmesh looks perfect.
So the real issue still seems to be this: Navmesh SamplePosition is returning values that are on the heightmesh, not the navmesh. And it seems to ignore Carved positions.
Okay so here’s my theory about what SamplePosition is doing:
- Searches for navmesh up and down in from the center first (as it states in the documentation)
- If it can’t find that, searches in the sphere as would make more sense for a 3D game
- Finds the navmesh point
- Converts that navmesh point to a HEIGHTMESH point
- returns the heightmesh point in the navmesh hit
Is this correct? That would explain the behavior I’m seeing. I get that they would return the heightmesh point because they assume that’s where you’d want to place your AI or whatever, but the problem with that is the heightmesh does not obey navmesh carving rules, which means you can’t exclude areas from the navmesh without separating out pieces of mesh manually, which is a huge pain.