- Home /
Can't get line renderer to stop at walls
I'm using a line renderer as a laser sight but can't figure out how to get it to stop at colliders. I'm using the below code
RaycastHit hit;
Vector3 aimDir = -transform.parent.TransformDirection(Vector3.up) * 100;
//Debug.DrawRay(transform.position, aimDir, Color.green);
if(Physics.Raycast(transform.position, aimDir, out hit, 1000f, 9)){
Debug.Log("laser sight hit " + hit.transform.position);
Debug.Log("laser sight hit " + hit.transform.name);
lineRenderer.SetPosition(1, hit.transform.position);
}
However as per the below screenshot it just goes backwards. I've confirmed aimDir is correct through Debug.DrawRay but the line renderer seems to go to a random position. What am I doing wrong?
Answer by sacredgeometry · Dec 06, 2020 at 04:56 PM
In your example you don't set position for index 0 ... you should probably set that to your guns laser pointers position as I think it would just get set to origin otherwise. i.e lineRenderer.SetPosition(0, positionOfStartOfLine);
Your answer

Follow this Question
Related Questions
How can I increase the view-distance of a trail renderer? 1 Answer
why can't Line Renderer catch movement of camera? 0 Answers
Draw out Line in C# 0 Answers
Lightning Using A Line Renderer 0 Answers