BOXCAST2D not returning consistent results

I have the below code to do a box cast.

In words,
My box cast starts right outside the player’s capsule collider.
The box is the same size as the capsule
The cast is done 2-3 units to the right and left of the player

Below is the code

    bool CheckObstruction(Vector2 p_direction, float p_distance, LayerMask p_layer, bool p_offsetCenter = false)
    {
        Vector2 center = capsuleCollider.bounds.center;

        if (p_offsetCenter)
        {
            //Sometimes the box cast should start from elsewhere
            if (p_direction == Vector2.left || p_direction == Vector2.right)
            {
                //Offset to right of left, so the cast starts from right/left border of the capsule
                center = center + (p_direction * capsuleCollider.bounds.size.x);
            }
            if (p_direction == Vector2.up || p_direction == Vector2.down)
            {
                //Offset to right of left, so the cast starts from right/left border of the capsule
                center = center + (p_direction * capsuleCollider.bounds.size.y);
            }

        }

        string direction = "";
        if (p_direction == Vector2.right)
        {
            direction = "Right";
            Debug.DrawLine(center, (center + Vector2.right * capsuleCollider.bounds.size.x/2), Color.red);
            Debug.DrawLine(center, (center + Vector2.left * capsuleCollider.bounds.size.x/2), Color.red);
            Debug.DrawLine(center+Vector2.up, center+Vector2.down, Color.red);
        }
        if (p_direction == Vector2.left)
        {
            direction = "Left";
            Debug.DrawLine(center, (center + Vector2.right * capsuleCollider.bounds.size.x/2), Color.green);
            Debug.DrawLine(center, (center + Vector2.left * capsuleCollider.bounds.size.x/2), Color.green);
            Debug.DrawLine(center + Vector2.up, center + Vector2.down, Color.green);
        }



        RaycastHit2D hit = Physics2D.BoxCast(center,
                                        capsuleCollider.bounds.size,
                                        0.0f, //Rotation of the box
                                        p_direction,
                                        p_distance, //Yo-yo to a certain distance in the given direction
                                        p_layer);
        if (hit)
        {
            if (p_offsetCenter)
            {
                print("Hit:" + hit.collider.gameObject + " to the " + direction);
            }
            return true;
        }
        else
        {
            if (p_offsetCenter)
            {
                print("No obstruction" + " to the " + direction);
            }
            return false;
        }
    }

But as can be seen from screenshot below, though there is wall to the right side, it is not getting detected by the BOXCAST2D. If I move the player slightly above, it is detected rightaway. The red-cross shows the starting point of the box cast.


The tiles are in a layer called LEVEL, and I am passing the exact same in the call to the method.

This didnt work because my box cast started entirely within a collider which was covered by the layer mask
I thought I have overcome this by using Physics2D.OverlapArea. But the same limitation applies there.