NavMesh ignores some of the MeshColliders

There’s a problem that I ran into recently. I’m working on a game where there are asteroids. Each asteroid has a MeshCollider which cuts holes in the NavMesh plane. The issue I ran into is that some of the asteroids would correctly cut a hole in the NavMesh, but some of the asteroids (using the exact same prefab) would not cut a hole in the NavMesh – which caused AI to run into them. Some of my code would adjust the localScale or rotation, and changing those values would cause a specific asteroid to toggle between “makes a hole” and “doesn’t make a hole” in the NavMesh. I’m pretty sure this is a bug in the engine that constructs the NavMesh combined with MeshColliders.

When I change the asteroids from MeshCollider to SphereCollider, everything works, which makes me think this problem is specifically related to the NavMesh + MeshColliders.

Here’s an example - the asteroid on the right side of the image does not cut a hole in the NavMesh, but the asteroid on the left (the exact same prefab) does cut a hole in the NavMesh.

Here’s the same image, but the asteroids are hidden - so that you can get a better view of the NavMesh:

I slightly altered the rotation of both of these asteroids by 1 degree. After the slight rotation, the right asteroid is cutting a hole in the NavMesh, but the left asteroid is not:

Here’s the same image, but the asteroids hidden - so that you can get a better view of the NavMesh:

I haven’t figured out any way to fix this except for switching to using SphereColliders, which seem to work reliably, although the sphere colliders don’t quite follow the contours of the asteroid.

My guess is that there’s some error in the Unity3d math which generates the NavMesh based on MeshColliders.

try looking at the options here maybe you have too many holes or the navmesh resolution needs to be changed.

https://docs.unity3d.com/Packages/com.unity.ai.navigation@2.0/manual/NavMeshSurface.html

I don’t know. I changed the NavMesh VoxelSize and TileSize. Neither had any effect.

try using the option where you specify the objects that need to be included instead of letting unity decides what to include, NavMeshModifier Component Only this one

and maybe your object has bad geometry somehow :man_shrugging: use a different one

try using the option where you specify the objects that need to be included instead of letting unity decides what to include

Not sure what you mean. Do you mean the NavMeshSurface option to only include gameObjects that are its children?

NavMeshModifier Component Only this one

Attach a NavMeshModifier to my asteroid? Are there any particular options?

maybe your object has bad geometry somehow :man_shrugging: use a different one

I’m having this problem with all eight of my asteroid models. My asteroid models are pretty simple - just spheres where I applied noise to the vertex locations to give each asteroid an irregular shape (done in 3dMax before being imported into Unity). I’m not doing anything crazy with my models.

It seems random whether any particular instance of an asteroid prefab actually cuts a hole in the NavMesh.

Here’s the settings for the NavMeshSurface and Asteroid gameObjects.


is in the link I’ve posted in the section Object collection

It looks like your link is for AI Navigation 2.0.5, and I’m still on 1.1.5, and 1.1.5 does not have the “NavMeshModifier Component Only” option.

Screenshot of what I’m looking at:

I checked my package manager, and it says I’m on 2.0.5, but those are the options for 1.1.5.

I removed this component and replaced it with “NavMesh Surface” (with a space), which does have the “NavMeshModifier Component Only” option. So, it’s version 2.0.5.

However, I can’t access the “NavMesh Surface” component in my C# code. It acts like the old 1.1.5 version is the only component that I can access. As a result, I can’t call the BuildNavMesh() method. Maybe Unity made a accidentally created a “NavMesh Surface” component instead of calling it “NavMeshSurface”. Still trying to figure out how to switch over to the 2.0.5 version of “NavMesh Surface”.

What I’m currently doing (which works with 1.1.5), but can’t figure out how to get the “NavMesh Surface” component:

UnityEngine.AI.NavMeshSurface oMeshSurface = gameObject.GetComponentInChildren<UnityEngine.AI.NavMeshSurface>();
Debug.Assert( oMeshSurface != null);
if( oMeshSurface != null) oMeshSurface.BuildNavMesh();

(I’m procedurally generating the gameObjects on the map, so I don’t know what the NavMeshSurface should look like until after I’ve procedurally generated the map. That’s why I have to call BuildNavMesh() in the code.)

well this is my end of knowledge on the topic :face_with_peeking_eye:

Ok. Thanks for your help.

.

For anyone reading this thread:

I switched the (2.0.5 version) NavMeshSurface to use “NavMeshModifier Component Only”. It didn’t fix anything.

.

Also, I was able to update from AI Navigation 1.1.5 to 2.0.5 by using a different namespace path. It still didn’t solve my problem, but this is how to switch your scripts to use the newer version of the NavMeshSurface.

Old Code (1.1.5 version of AI Navigation components):

UnityEngine.AI.NavMeshSurface oMeshSurface = gameObject.GetComponentInChildren<UnityEngine.AI.NavMeshSurface>();
Debug.Assert( oMeshSurface != null);
if( oMeshSurface != null) oMeshSurface.BuildNavMesh();

New Code (2.0.5 version of AI Navigation components):

Unity.AI.Navigation.NavMeshSurface oMeshSurface = gameObject.GetComponentInChildren<Unity.AI.Navigation.NavMeshSurface>();
Debug.Assert( oMeshSurface != null);
if( oMeshSurface != null) oMeshSurface.BuildNavMesh();

.

I did figure out that if I set the MeshCollider to uncheck “Convex” that it correctly cuts a hole in the NavMesh. (I’m unclear why being convex hurts anything. I would think that convex would be easier to calculate and concave would be more the more computationally difficult.) The downside is that Rigidbodies don’t work with concave MeshColliders. So, I had to remove the rigidbodies.

So, that’s one possible solution to the “MeshColliders sometimes don’t affect the NavMesh” problem. If you’re okay with not using RigidBodies for those objects, it works.