Hi, trying to make my 2d raycasts bounce off the surface, but reflection is wrong and i think this is because of my raycast normal points to center and does not stay perpendicular. All of the objects are on the same layer and raycasting works fine, except normal and reflection angle. Normal is the red line in the picture.
Code:
[HideInInspector]
public LineRenderer lineRenderer;
private RaycastHit2D hit;
private RaycastHit2D hitOne;
private RaycastHit2D hitTwo;
private Vector2 twoDir;
private Vector2 wallNormal;
public List<Transform> LaserHit;
private int LayerMask = 1 << 12;
public bowScript bs;
void Start()
{
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.enabled = false;
lineRenderer.useWorldSpace = true;
}
void Update()
{
if (bs.sArrowSelected)
{
hit = Physics2D.Raycast(transform.position, transform.right, Mathf.Infinity, LayerMask);
LaserHit[0].position = hit.point;
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, LaserHit[0].position);
//find normal
wallNormal = hit.normal;
//find a reflected vector
twoDir = Vector2.Reflect(LaserHit[0].position, wallNormal).normalized;
//shoot second raycast towards twoDir direction
hitOne = Physics2D.Raycast(LaserHit[0].position, twoDir, Mathf.Infinity, LayerMask);
//setting the second position transform as a reflected raycast's position
LaserHit[1].position = hitOne.point;
//setting the second point of linerenderer go towards reflected position
lineRenderer.SetPosition(2, LaserHit[1].position);
Debug.DrawLine(transform.position, LaserHit[0].position);
Debug.DrawLine(LaserHit[0].position, LaserHit[1].position);
Debug.DrawLine(LaserHit[0].position, wallNormal, Color.red);
Debug.Log("wallnormal " + wallNormal);
}