- Home /
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 )
Upd: If the line isn't hitting the colliders, it works fine ( lr.SetPosition(1, new Vector3(0,0,5000)); )
Your code looks fine. $$anonymous$$aybe try to use the Script in a $$anonymous$$imal example and see if it works.
If it does, copy your scene and remove parts of it step by step until it works properly to find out what makes it break.
Answer by IvovdMarel · Nov 13, 2017 at 03:39 AM
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.
I usually would agree, however if the raycast is done from the same object where the line renderer is attached to using local space should work this way. "distance" would be the localspace distance. When using localspace you don't need to update the start point of the line renderer all the time as in localspace it's always 0,0,0.
.
Though if the raycast is actually done from the same object we don't know for sure as this information is missing from the question. If it's actually using the correct space and it gives the wrong distance, the ray most likely didn't hit the object he thinks that he's hitting. Printing the object name that was hit would probably help.
Local space is absolutely fine for the line renderer in this case, and there is no reason to not use hit.distance.
hit.distance is the distance from the ray origin to the hit point, not the distance between two objects.
Answer by Max_Bol · Nov 13, 2017 at 11:43 AM
The mistake you made is that you don't use LineRenderer().SetPosition() correctly. https://docs.unity3d.com/ScriptReference/LineRenderer.SetPosition.html
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)
This is incorrect. The LineRenderer is set to local space, which means that the LineRenderer with position 0 being 0,0,0 starts at transform.position, and the second position is, in world space, "transform.position + transform.forward * hit.distance", which is correct.
Your answer
![](https://koobas.hobune.stream/wayback/20220612142718im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycasts in Multiplayer Network 0 Answers
[C#] Line Renderer beam not going straight 1 Answer
c# Raycast going off at odd angles. Unity 5 2 Answers
Simple wire/cable 1 Answer
RayCast Line Not Rotating with Gun? 0 Answers