Question by 
               Metabeta · Mar 01, 2021 at 12:56 PM · 
                raycastvector2raycasthit2dreflect  
              
 
              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.
what I want 
what I dont want 
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);
                 }
             }
         }
     }
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Real concept of Vector2.Reflect 0 Answers
How to Raycast 2D Diagonally instead of Horizontal on a Platformer game. 0 Answers
Vector2.Reflect returns opposite reflection 0 Answers
How can I detect which game object was clicked? 0 Answers
Need help ironing out this weird error in Raycast2D and Vector2.Reflect, for a billiard game test 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                