4
\$\begingroup\$

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.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Are you sure Use world space is unchecked in the LineRenderer component? \$\endgroup\$
    – Hellium
    Nov 9 '17 at 17:01
  • \$\begingroup\$ @Hellium I just edited the question \$\endgroup\$
    – chronos
    Nov 9 '17 at 17:14
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ I just updated the question. \$\endgroup\$
    – chronos
    Nov 9 '17 at 17:33
1
\$\begingroup\$

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>();
     var meshFilter = lineObj.AddComponent<MeshFilter>();
     Mesh mesh = new Mesh();
     lineRenderer.BakeMesh(mesh);
     meshFilter.sharedMesh = mesh;

     var meshRenderer = lineObj.AddComponent<MeshRenderer>();
     meshRenderer.sharedMaterial = s_matDebug;

     GameObject.Destroy(lineRenderer);
 }

Make sure lineObj has LineRenderer component.

\$\endgroup\$
0
\$\begingroup\$

Either update the line renderers positions yourself via script, or use a Trail Renderer

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .