Raycasting will not hit some imported meshes with mesh colliders?

Using if (Physics.Raycast (ray, out hit, etc, etc) {

and some code, the raycasts are ignoring mesh collider. Tested using a cube in the same position, and it works. Yes I am using appropriate layer for both that I have set up. Makes no sense. Normals of mesh are not reversed because the mesh is shaded with texture correctly. It would be invisible from top if normals were flipped. Makes no sense. Can someone do a simple test? It is an imported non primitive shape. It is terrain shaped.

EDIT: Did a test in a new project/scene. Used imported meshes and it worked, they were pretty basic, a cube, a plane, and a subdivided plane (very low triangles though). Tried importing the original mesh I was working with, which is about 8000 tris, 4500 verts, still does not get hit by the raycast. I am wondering if there is a hidden limit enforced on raycasting topology? Or maybe there is something wrong with the mesh somehow?

After a lot of tests, I believe it is the mesh. I don’t understand what could be wrong with it. Especially when it still collides with rigidbodies, and the normals are facing correctly. What all could be wrong with any given mesh that I can look into?

Mesh colliders tend to be less precise/efficient than native colliders. Also they have special rules compared to natives.

From: Unity - Manual: Mesh Collider component reference

Collision meshes use backface culling. If an object collides with a mesh that will be backface culled graphically it will also not collide with it physically.

There are some limitations when using the Mesh Collider. Non-convex Mesh Colliders are only supported on GameObjects without a rigidbody. If you want to use a Mesh Collider on a rigidbody, it needs to be marked as Convex.

Try to mark your collider as convexe (if it make sense in your particular case). Convexe mesh colliders behave more logicaly.


Are you logging what is being hit by the ray? It could in my experience be hitting something else. Whatever the issue, I can test if you’d like, can you send a link to the proj? I’m running hundreds of meshes in an area and getting hit data in my own proj :slight_smile: