- Home /
Line renderer positioning
Hi, I am having some difficulties with the positioning of the line renderer.
As you can see here, i want the pink line (line renderer) to follow the same path as the black line (raycasting). I have used the same positioning code for both only the Pink line doesn't really listen. I have already changed the UseWorldSpace to false, but that didn't work out either.
This is the code that I'm using to set the positions.
void Update () {
Laser ();
}
void Laser(){
int maxVertCount = 4;
lineRenderer.SetVertexCount (maxVertCount);
RaycastHit hit;
Vector3 pos;
lineRenderer.SetPosition (0, this.transform.position);
for (int i = 0; i<maxVertCount; i++) {
if(Physics.Raycast(transform.position,Vector3.right,out hit,Mathf.Infinity)){
Debug.DrawRay (this.transform.localPosition, Vector3.right * hit.distance , Color.black);
pos = Vector3.Reflect (hit.point - transform.localPosition, hit.normal);
//Debug.DrawRay (hit.point, pos , Color.black);
lineRenderer.SetPosition (1, pos);
Debug.DrawRay (hit.point, pos , Color.black);
}
}
}
Answer by ChrisJoosten · Mar 10, 2014 at 01:13 PM
I found the problem.
It was changing its position from the start and not from the hitpoint.
So i changed a few lines. Now I have set an extra position point at the Hit position. Now It's functioning like I wanted it! :)
void Update () {
Laser ();
}
void Laser(){
int maxVertCount = 3;
lineRenderer.SetVertexCount (maxVertCount);
RaycastHit hit;
Vector3 pos;
Vector3 pos2;
lineRenderer.SetPosition (0, transform.localPosition);
if(Physics.Raycast(transform.position,Vector3.right,out hit,Mathf.Infinity)){
lineRenderer.SetPosition(1,hit.point);
lineRenderer.SetPosition (2, Vector3.Reflect(hit.point - transform.localPosition, hit.normal));
//pos = Vector3.Reflect (hit.point - transform.localPosition, hit.normal);
}
}
Answer by Berenger · Mar 10, 2014 at 10:16 AM
You're always changing the second point of the line renderer, instead of incrementing them. It should be lineRenderer.SetPosition (i, pos);. Make sure you create enough with SetVertexCount though.
No, that doesn't help either.. When i change it to lineRenderer.SetPosition (i, pos); ins$$anonymous$$d of 1, It doesn't show any line.. I also changed the first one with the 0 to i. But nothing happened.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to Change the position of a tile to the player position? 0 Answers