Raycast not always detecting object

Hello, I’m having a problem with raycasts. The raycast only detects both cubes when it’s really near the center of 2 cubes, while I want it to detect the whole collider of a cube.

Here’s the code in question:

void RaycastChecker(RaycastHit hit)
    {
        Vector3 origPosition = Input.mousePosition;
        Vector3 firstBlockCentrePos = new Vector3(origPosition.x - 0.5f, origPosition.y, origPosition.y);
        Vector3 secondBlockCentrePos = new Vector3(origPosition.x + 0.5f, origPosition.y, origPosition.y);
        Ray firstBlockRay = Camera.main.ScreenPointToRay(firstBlockCentrePos);
        Ray secondBlockRay = Camera.main.ScreenPointToRay(secondBlockCentrePos);
        RaycastHit hitFirstBlock;
        RaycastHit hitSecondBlock;
        int layermask = 1 << 9;
        if (Physics.Raycast(firstBlockRay, out hitFirstBlock, Mathf.Infinity, layermask) && Physics.Raycast(secondBlockRay, out hitSecondBlock, Mathf.Infinity, layermask))
        {
            Debug.DrawRay(hitFirstBlock.transform.position, hitFirstBlock.transform.TransformDirection(hitFirstBlock.transform.forward) * 10, Color.yellow);
            Debug.DrawRay(hitSecondBlock.transform.position, hitSecondBlock.transform.TransformDirection(hitSecondBlock.transform.forward) * 10, Color.yellow);
            Debug.Log("Hit 2 objects: " + hitFirstBlock.collider.gameObject.name + " and " + hitSecondBlock.collider.gameObject.name);
            Debug.Log(hitFirstBlock.transform.position);
            Debug.Log(hitSecondBlock.transform.position);
        }
    }
}

I dont really understand the way you are creating your Rays for the Raycast. Debug.Draw your firstBlockRay and secondBlockRay, before you even Raycast, to see if they are doing what you are expecting.

If the rays are doing what you are expecting, then try to instead of using Mathf.Infinity, use just 9999 or something not crazy high, since I found in the past that Mathf.Infinity causes issues, though that was just with a spherecast, not sure about raycast.
Heres the bug report Unity Issue Tracker - Windows-only: SphereCast with Math.Infinity as the cast length misses hits