how to detect an intersecting collider with raycastHit2D and switch the end point to the collider hit point

my RayCastHit2D only detects collision on the end point when using mouse position and when i move far away from the object with collider or cause the object to intersect the raycast midway it. how do i detect collisions that along the RaycastHit2D line.

this is the code from the script which is called from a FixedUpdate method:

    public Camera cam;
    public LineRenderer lineRenderer;
    public Transform firePoint;

var mousePos = (Vector2)cam.ScreenToWorldPoint(Input.mousePosition);
       
        lineRenderer.SetPosition(0, firePoint.position);
        lineRenderer.SetPosition(1, mousePos);
       
        Vector2 direction = mousePos - (Vector2)transform.position;
     
        RaycastHit2D hit = Physics2D.Raycast((Vector2)transform.position,direction.normalized,direction.magnitude);
      

        if (hit)
        {
            lineRenderer.SetPosition(1, hit.point);

            Platform2Handler target = hit.transform.GetComponent<Platform2Handler>();

            if (target != null)
            {
              // switch here is called from another script that just switches certain objects with another every time the raycast hits.
                target.Switch();
            }


        }

so i know i need to callculate the point where the line intersects with a collider regardless of where the mouse position but how to do that i have no idea. and the other issue might be fixed from the previous issue, why is the lineRenderer detecting the collider at the end point but when i move the end point far away the line just goes through it.

Ok so i figured out the 1st issue which was that i needed to change this part:

if (hit)
{

}

to this:

if (hit.collider !=null)
{

}

but the issue is that my laser still passes a collider on corners