Raycast never returns true (even when the ray directly intersects with the mesh collider)

I’ve been puzzling over this one for a while now.

I’m creating a drawing application, where the lines are represented by a Mesh with a MeshCollider. The eraser essentially casts rays in a circular area around the mouse when it is used, however the Raycast never returns true, even though the rays appear to intersect the mesh as shown here:

Here is the relevant code:

void drawEraserCircle(Vector3 mousePos)
{
    //Heavy perf hit
    //Ideas for optimization: 1) On first mouse click cast rays across entire circle, afterwards only cast rays on the perimeter (where new meshes would be encountered)
    // 2) Cast rays every couple pixels instead of every pixel
    float radius = 10;
    int counter = 0;
    for (float y = mousePos.y - radius; y < mousePos.y + radius - 2; y += 2) {
        for (float x = mousePos.x - radius; x < mousePos.x + radius - 3; x += 3) {
            counter++;
            
            float deltaX = mousePos.x - x;
            float deltaY = mousePos.y - y;
            double distance = Math.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
           // Debug.Log("D " + distance);
           //Debug.Log(counter + " X " + x + " Y " + y + " D " + distance + " M " + mousePos);
            //Point lies outside of circle
            if (distance - radius > 1) {
                continue;
            }

            //Edge threshold
            if ((radius / distance) < 0.9) {
                continue;
            }
        
            //Cast ray to point on circle
            Vector3 rayPos = new Vector3(x, y, -100);
            Debug.Log(rayPos);
            castMeshRay(rayPos);
        }
    }
}

void castMeshRay(Vector3 rayPos)
{
    RaycastHit hit;
    Ray ray = new Ray(rayPos, Vector3.forward);
    Debug.DrawRay(ray.origin, ray.direction*100, Color.red, 20f, false);
    //If ray hits a mesh triangle, delete it
    if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity, 1<<6)) //ray never hits???
    {
        Debug.Log("hit");
        selectedMesh = hit.transform.gameObject;
        deleteTri(hit.triangleIndex);
    }
}

Any suggestions are greatly appreciated, thanks!

Turns out MeshColliders don’t automatically update as the mesh is changed. I was adding the component when the Mesh was first created instead of after it was done updating. The collider simply never updated.

@Unshelled, I am not sure but you need to have collider on your mesh. I would suggest not to use mesh collider, you permeative collider for ex. box collider.

I appreciate the feedback, but I’m not sure how I could achieve the same accuracy without a mesh collider as the collider needs to be very accurate for the eraser to work properly.