I’m using a LineRenderer to trace a laser. The second point of the LineRenderer has to be wherever it’s coming in contact with a collider, and if it’s not contacting one, it should maintain a set length.
This isn’t working at all. It’s just INCREDIBLY unstable and makes these weird, random movements (the end point sways around at random).
Not sure what I’m doing wrong here, as the code is as basic as it gets.
It pretty much works the way I need it to, except that as my character moves left and right, the end point of the laser sways GREATLY left and right, resulting in a laser that won’t stay straight unless I’m in the middle of the screen.
void SetLength()
{
RaycastHit LaserHit = new RaycastHit();
if (Physics.Raycast(new Ray(gameObject.transform.position, -Vector3.forward), out LaserHit))
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
if (LaserHit.collider.tag == "Enemy")
{
SetLaserEndpoint(gameObject.transform.position + (new Vector3(0, 0, -LaserHit.distance)));
}
}
else
{
SetLaserEndpoint(gameObject.transform.position + (new Vector3(0,0,-10)));
}
}
void SetLaserEndpoint(Vector3 point)
{
lineRenderer.SetPosition(1, point);
}