Raycast2d does not always work properly

I’m trying to use the vector2.Reflect with raycast. Depending on the angle or, it sometimes reflects the way I want, sometimes it does not. Is there a place where I misspelled?

What I want (when working properly):

what I dont want (while not working properly)
6889727--805628--12.png

Here is my code:

 void Update()
     {
         var ray = new Ray(transform.position, transform.right);
         hit = Physics2D.Raycast(ray.origin, ray.direction,10f, LayerMask.GetMask("Wall"));
         Debug.DrawRay(ray.origin, ray.direction*10, Color.red);
         if (hit.collider)
         {
             var ray2 = new Ray(hit.point, Vector2.Reflect(ray.direction, hit.normal));
             Debug.DrawRay(ray2.origin, ray2.direction * 10, Color.green);
             RaycastHit2D hit2 = Physics2D.Raycast(ray2.origin, ray2.direction, 10f, LayerMask.GetMask("Wall"));
             if (hit2.collider)
             {
                 var ray3 = new Ray(hit2.point, Vector2.Reflect(ray2.direction, hit2.normal));
                 Debug.DrawRay(ray3.origin, ray3.direction * 10, Color.blue);
                 RaycastHit2D hit3 = Physics2D.Raycast(ray3.origin, ray3.direction, 10f, LayerMask.GetMask("Wall"));
                 if (hit3.collider)
                 {
                     var ray4 = new Ray(hit3.point, Vector2.Reflect(ray3.direction, hit3.normal));
                     Debug.DrawRay(ray4.origin, ray4.direction * 10, Color.magenta);
                 }
             }
         }
     }

I wonder if subsequent raycasts are emitting from within the first collider you hit, just because that hit point is precisely mathematically at the surface of the collider.

You could try moving it outwards a smidge, using the normal times a very small amount, like 0.001f, just to experimentally see if the issue goes away.

You can also print the name of the collider you hit, so you can make the names unique and track down what you are hitting.

Alternatively you could hide the collider you just hit when you raycast from a hit on it, then re-enable it at the end.