Raycast2d normal points to the center of the world
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.
ScreenShot: https://imgur.com/KmPWJty
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);
}
Answer by MontigorJ · Jul 05, 2020 at 01:45 PM
Okay, looks like i solved this problem. First of all it was a stupid idea to debug normal vector with drawline, since it takes vector start and vector end parameters but not the direction. I used drawray to debug. I decied to shoot a ray (Ray2D) from my bow and make Vector reflect work with ray's direction. It works.
hit = Physics2D.Raycast(bs.ray.origin,bs.ray.direction, 50f,LayerMask);
//find normal
Vector2 wallNormal = hit.normal;
//find a reflected vector
twoDir = Vector2.Reflect(bs.ray.direction, wallNormal).normalized;
//shoot second raycast towards twoDir direction
hitOne = Physics2D.Raycast(hit.point, twoDir.normalized, 50f, 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, Color.green);
Debug.DrawLine(LaserHit[0].position, LaserHit[1].position, Color.blue);
Debug.DrawRay(LaserHit[0].position, wallNormal, Color.red);
Debug.Log("twodir " + twoDir);`
Your answer
Follow this Question
Related Questions
raycast : get normal value of linecast hit 1 Answer
Physics2d.Raycast always returning 0,0 0 Answers
Real concept of Vector2.Reflect 0 Answers
raycasthit.normal sometimes returns zero 1 Answer
Raycast2d does not always work properly 0 Answers