Ray cast on mesh collider instance not working

Hello,

Ok so basically in my level start script I piece together many different map pieces and then combine them into a single mesh. After this is complete I turn off the individual pieces and add a mesh collider to the single piece I made earlier.

In my camera script I have a grid projector and a tile select projector which follows my mouse around on the screen.

Here is the problem… The ray being cast to track the mouse movements detects the base plane under my scene which has a mesh collider but has “Plane” as the mesh. Over this appears my newly created mesh as described above also with a mesh collider but it has “instance” as the mesh in the collider. The ray does not seem to recognize this at all and goes right through it to the plane below. If instead of giving my newly created mesh a mesh collider but instead a boxcollider it works fine.

Box collider would be nice except I plan on having multiple layers of 3d tiles and the top collider would end up messing up all the layers below it thus the need for a mesh collider on each layer of tiles. But it seems Physics.Raycast will not detect a collision with an instanced mesh collider.

Anyone else run into this or know of a work around? Thanks!

Well so it turns out that if you add a meshcollider component to an instance of a mesh after combing children that a Raycast won’t hit it.

In order to fix the problem what I needed to do was make sure the meshes I was instantiating had generate colliders turned on in the import settings of the file themselves.

What is interesting is that even though the objects are all turned off and there is no collider on the parent mesh which has been created by combining all the children the collider still exists in the scene anyways.

I realize this is very confusing but it anyone else runs into this problem then you will know what I did to work around it.

Answer in a nut shell… If raycast doesn’t hit an instance mesh then make sure the meshes being used to create it are importing with the generate colliders option checked on. ^^b

4 Likes

thank you for sharing this :slight_smile:
Saved me a lot of time

Thanks heaps man, that saved me a lot of time too

Thanks! :slight_smile:

I know this an semi old thread, but I found that if a collider is a child of a gameobject it returns the parents gameobject and transform. So when checking tags and layers.

RaycastHit hit;

if(Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity){
    if(hit.collider.tag == "TagOfObject"){
        print("hit child collider");
    }

    if(hit.collider.layer == LayerOfObject){
        print("hit child collider");
    }
}

Hope this helps!

Man, thanks a lot. I found this after hours of trying to find out what was wrong. A great resource.

Thank you so much, you saved my buttcheeks on a Sunday night!

can you be clearer please ? i have the same issue, but i dont understand your solution

@U2 Thanks for this, it was very much a weird issue for me and I was having trouble fixing it!