I’m shooting a linerender beam out of the foward position of a gameobject and if it hits any object with the tag “Mirror” I want it to bounce off at the correct angle. However, there’s something wrong with my laser reflect code, and I’m inexperienced in this aspect of Unity. For some reason, when the laser hits an object with a 45 degree angle on it, it seems to pass through the mirror object and shoot off at a larger angle. View my code below:
void RenderLaser(){
laserLine.SetColors (color, color);
laserLine.SetPosition (0, transform.position);
laserRay.origin = transform.position;
laserRay.direction = transform.forward;
if (Physics.Raycast (laserRay, out laserHit, range)) {
laserLine.SetPosition (hitCount, laserHit.point);
if(laserHit.collider.tag == "Mirror"){
hitCount += 1;
laserLine.SetVertexCount(hitCount+2);
laserLine.SetPosition (hitCount, laserHit.point);
Debug.Log(transform.position);
Debug.Log(laserRay.origin);
Debug.Log(laserHit.normal);
Debug.Log(laserHit.point);
Vector3 reflectPos = Vector3.Reflect(transform.position, laserHit.normal);
hitCount += 1;
laserLine.SetPosition(hitCount, reflectPos);
}
} else {
laserLine.SetPosition (hitCount, laserRay.origin + laserRay.direction * range);
return;
}
}
Thanks in advance!