Raycast.distance giving me the wrong number

Aloha, i am trying to build a simple billiard and right now i am stuck on a problem with a Raycast… the output of a Raycast.distance is wrong…

That’s the part of my code

void Update () {
		RaycastHit hit;
		if (Physics.Raycast(transform.position, transform.forward, out hit))
		{
			if(hit.collider)
			{
				lr.SetPosition(1,new Vector3(0,0,hit.distance));
			}
		} else{
			lr.SetPosition(1, new Vector3(0,0,5000));
		}
	}

So what am i doing wrong?

I wann that the Line will stop at the target (that why i am using hit.distance )

You should use hit.point ( the point where it hit), not hit.distance (the distance from your object to the object that was hit).

In addition, you should check the ‘Use World Space’ checkbox.

The mistake you made is that you don’t use LineRenderer().SetPosition() correctly.

LineRenderer().SetPosition() requires you to fill it with the the integer as Index (this you did) and a Vector3 as position (which you didn’t as you feed it the distance… not the position you want the line to end up at).

You should feed it hit.point instead of hit.distance as this give the position (vector3) where the raycast hit.

The mistake here was that you handle the LineRenderer as if it was a Raycast (which is done toward a direction and a distance)