- Home /
LineRenderer. Problem with ray displaying
Hello. What am I doing wrong? Here is the code and screenshots with my problem( When the beam hits the object, it is reduced. P.S. Im not use WorldSpace coordinate
public class Emitter : MonoBehaviour {
private LineRenderer lr;
void Start ()
{
lr = GetComponent<LineRenderer>();
}
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));
print("Hit: " + hit.distance);
}
}
else
{
lr.SetPosition(1, new Vector3(0, 0, 100));
}
}
}
if the LineRenderer is using WorldSpace replace lr.SetPosition(1, new Vector3(0, 0, hit.distance));
with lr.SetPosition(1, hit.point);
.
Else replace lr.SetPosition(1, new Vector3(0, 0, hit.distance));
with lr.SetPosition(1, lr.transform.InverseTransformPoint(hit.point));
.
Thanks for the tip, but I forgot to say that do not use WorldSpace.
Answer by Fliperamma · Oct 30, 2016 at 08:43 PM
Since you are not using WorldSpace in Line Renderer, you'll need to convert the end position/distance to the Local Space. Something like this:
float dist = transform.InverseTransformVector(transform.position - hit.point).magnitude;
lr.SetPosition(1, new Vector3(0, 0, dist));
Thank you. Works great! But it is strange. I looked and read a lot of lessons and everywhere works as I did in the beginning. $$anonymous$$aybe the information is outdated. Thanks again!
Answer by Lishin · Oct 30, 2016 at 08:21 PM
Update: If i use debug.drawray - hit.distance work perfectly
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
Vector3 forward = transform.TransformDirection(Vector3.forward) * hit.distance;
Debug.DrawRay(transform.position, forward, Color.green);
...
Your answer
Follow this Question
Related Questions
Test line renderer collision? 1 Answer
How to check of a line renderer collides with ground ? 1 Answer
How to draw a line between objects and get distance 1 Answer
Creating a Ray Gun 1 Answer