- Home /
The question is answered, right answer was accepted
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?
Answer by QuentinLG · Dec 04, 2016 at 08:32 PM
Mesh colliders tend to be less precise/efficient than native colliders. Also they have special rules compared to natives.
From: https://docs.unity3d.com/Manual/class-MeshCollider.html
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.
Raycasting only works against colliders, so it's possible that it shares the same weaknesses.
Fixed this issue a while back. Sadly I don't remember what it was, so I'll mark your answer correct because it is relevant. I think I reimported them or something to fix it. Actually it might have been floating point issue of where the mesh was placed, don't remember.
Holy crap, this is indeed an issue. We searched for hours trying to figure out why our debug raycast was clearly going through a plane but the plane's collider.raycast was giving false
for the same ray.
Answer by drivenbeyond · Feb 28, 2016 at 02:23 PM
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 :)
It isn't hitting anything, I have a debug right after if(Physics.Raycast . The mesh collider is the only collider it could hit at this position. Like I said with a primitive from unity it works fine. Are the meshes in yours imported? are they primitives? Primitives from unity work with box or mesh collider for me, but imported meshes it just doesnt like apparently.
I just tested a box collider on the imported mesh just to make sure something wasnt super wrong, but it works. So yeh, imported mesh with mesh collider is broken for me.
and yet, using some spheres with rigidbodies, they properly collide with the mesh and roll around.
Follow this Question
Related Questions
What is wrong about this RaycastHit Event??? 1 Answer
Having a RaycastHit event create a component? 1 Answer
Raycasting help 2 Answers
Trouble with checking Raycast 1 Answer
Raycast not working 2 Answers