Raycast doesn't hit Mesh Collider

Hi all,

Not sure if this should be in graphics forum or here so apologies of it’s in the wrong place. I have seen a few old posts with a similar issue to this and tried a bunch of things but just can’t figure out why this doesn’t work.

I have a custom generated mesh (a tiled hex map). I want to use a ray to fire straight down onto the map and get the hit point (which will be used to place a tree - the red sphere in the images below).

Here is the code section:

    public void CreateTree(MapCell cell)
    {
        int height = 200;

        RaycastHit hit;
        Ray ray = new Ray(new Vector3(cell.transform.position.x, height, cell.transform.position.z), Vector3.down);
        // Debug version
        // RaycastHit hit;
        // Ray ray = new Ray(new Vector3(0, 20, 0), Vector3.down);

        if (Physics.Raycast(ray, out hit))
        {
            Debug.Log("## Hit point! " + hit.point);

            Tree tree = Instantiate(treePrefab);
            tree.transform.position = hit.point;
        }
        else
        {
            Debug.LogError("Tree raycast failed");
        }

    }

Sadly the Ray never hits the mesh :frowning:

This is a gizmo showing the source and direction of the Ray. Doesn’t hit the mesh…

If I add a plane into the scene above the mesh, the Ray hit the plane just fine. See the “tree” (red sphere) created at the hit point.

If I put a box collider on each hex, the Ray also hits.

But I can’t get the Ray to hit the MeshCollider. :face_with_spiral_eyes:

I tried making it Convex, didn’t work. The Cooking Options are set to default, isTrigger is off. I am setting the shared mesh like this:

            meshCollider.sharedMesh = null;
            meshCollider.sharedMesh = hexMesh;
            meshCollider.convex = true;
            meshCollider.enabled = true;

I’m sure normals are facing the right way (up). I’ve also tried firing a ray “up” from below the mesh, didn’t work.

Any ideas??

Thanks.

1 Like

OK so after a few more hours of debugging, I have worked out that the problem doesn’t appear to be anything to do with the mesh or collider… rather it’s a timing issue.

I am instantiating the mesh in a Start() and then trying to do the RayCasts in the Start() of another component. The mesh is created first then calls the other component so I figured that would be fine however for some reason that I don’t understand that seems to be an issue.

If I try RayCasts later (like in the next frame) it seems to work. So seems I need to let the mesh be created and updated in one frame and then possibly wait for the next frame to do the raycasts.

2 Likes

Yep that seems to be the issue. After building the mesh and updating normal, I found that if I then use a co routine to wait for the next frame, and THEN do then raycasts it’s all good. Not exactly sure of the details as to why that was required in my situation but hopefully posting this here might help someone.

2 Likes

I can guess it actually starts working after the next FixedUpdate, not Update, so depending on your fps it might be not ready in the next Update. You better test this

1 Like

this information is quite important to me… thank you so much

It is a necro and being a “top thread if you do a google search” is irrelevant. It’s the content that’s most important if you do necro.

I’ll not delete your post because it’s very important to understand that your “general solution” here is, at best, a completely unnecessary workaround.

The problem in this thread highlights a misunderstanding of the effects of changing a Transform i.e. you shouldn’t be doing that when there’s no need. If you do, nothing will get updated until the next simulation step and just calling SyncTransforms is a brute-force workaround that should not be required.

In this context, provide the position/rotation to the Instantiate call and you won’t need to modify the Transform and especially not a call to SyncTransforms as it’ll be created at that position/rotation.

This:

Tree tree = Instantiate(treePrefab);
tree.transform.position = hit.point;

… should be this:

Tree tree = Instantiate(treePrefab, hit.point, Quaternion.identity);

This works because the Transform is set before the cloned object is activated. When it’s activated, everything including the physics components are created at that Transform pose.

2 Likes

Thread Closed.