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)
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);
}
}
}
}