- Home /
how to make my LAZER-SIGHTS straight ('-' )
I have been experimenting with the line renderer for some time, got it to the point where a raycast goes off from an origin and hits a collider, that way I got 2 points to draw the line. My really problem is that when I put a max distance on the raycast and it DOESN'T hit a collider, then the end point of laser just goes off to some fixed point on the map.
So how do I keep the laser straight?
This is my code:
public float maxDistance = 100f;
private LineRenderer IR;
void Update () {
RaycastHit rayHit;
Physics.Raycast (transform.position, transform.forward, out rayHit, maxDistance);
IR.SetPosition (0, transform.position);
if (rayHit.collider)
IR.SetPosition (1, rayHit.point);
else
IR.SetPosition (1, transform.forward * maxDistance);
}
Comment
Your answer