# How to move a Line Renderer as its game object moves

I created an empty game object and added a Line Renderer component to it through a script. I thought that once I adjust the position of the game object, it would do likewise for the line rendeder, but that is not the case.

I tried useWorldSpace = false but it totally changed the positions making the following points a straight line, though I was able to move the game object to to the line.

public Vector3 beginPos = new Vector3 (-1.0f, -1.0f, 0);
public Vector3 endPos = new Vector3 (1.0f, 1.0f, 0);


Is there a way I can convert points to input what I am more familiar with (so that points like the above points don't create a straight line) or should I be approaching the problem in a different way? I am open to a different approach.

UPDATE

I think I left an import detail. I'll be using lines made with line renderer to create shapes. So I need to be able to be able to move the lines around to move the shapes so I don't have to always recalculate the start and end points for the lines.

• Are you sure Use world space is unchecked in the LineRenderer component? Nov 9 '17 at 17:01
• @Hellium I just edited the question Nov 9 '17 at 17:14

First of all make sure your LineRenderer component use World Position. then in your Update() method just assign points positions (this code works for 5.6.0):

void Update(){
if (usingLineRenderer)
{
myLineRenderer.SetPosition(0, transform.position);
if (hitBlocked == true) // we've hit something, so our line renderer end point should stop here
{
myLineRenderer.SetPosition(1, Hit.point);
}
else
{
myLineRenderer.SetPosition(1, transform.position + transform.forward * maxLaserDistance);
}
}

if(Physics.Raycast(transform.position, transform.forward, out Hit, maxLaserDistance, ~ignoredLayers)){
hitBlocked = true;
// do stuff
}
}


Particularly useful when using Virtual Reality Motion controllers, just assign it to the associated Hand Anchor.

• I just updated the question. Nov 9 '17 at 17:33

According to this post, you can bake the line into mesh, then you can move the line (as mesh), here is the code from the post:

 public static void BakeLineDebuger(GameObject lineObj)
{
var lineRenderer = lineObj.GetComponent<LineRenderer>();
Mesh mesh = new Mesh();
lineRenderer.BakeMesh(mesh);
meshFilter.sharedMesh = mesh;


Make sure lineObj has LineRenderer component.