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);
			}
		}
	}

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.

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! :slight_smile:

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);


		}
	}