0

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
chronos
  • 517
  • 1
  • 5
  • 14

2 Answers2

4

You just need to apply an offset to it. Get the line renderer position + the current position of the GameObject in the Start function. Apply that offset to the LineRender in the Update function.

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

Vector3 beginPosOffset;
Vector3 endPosOffset;

LineRenderer diagLine;

void Start()
{
    diagLine = gameObject.AddComponent<LineRenderer>();
    diagLine.material = new Material(Shader.Find("Sprites/Default"));
    diagLine.startColor = diagLine.endColor = Color.green;
    diagLine.startWidth = diagLine.endWidth = 0.15f;

    diagLine.SetPosition(0, beginPos);
    diagLine.SetPosition(1, endPos);

    //Get offset
    beginPosOffset = transform.position - beginPos;
    endPosOffset = transform.position - endPos;
}

void Update()
{

    //Calculate new postion with offset
    Vector3 newBeginPos = transform.position + beginPosOffset;
    Vector3 newEndPos = transform.position + endPosOffset;

    //Apppy new position with offset
    diagLine.SetPosition(0, newBeginPos);
    diagLine.SetPosition(1, newEndPos);
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Interestingly, I've been working with the solution to [How to extend diagonal line renderer line over a certain duration](https://stackoverflow.com/questions/47145383/how-to-extend-diagonal-line-renderer-line-over-a-certain-duration/47146026#47146026) which you answered. When I use this solution to change the position of the line renderer and I try extending it, I get some strange behavior with the line renderer appearing at the offset position and returning to the original position. – chronos Nov 09 '17 at 19:32
  • Sorry this solution is not meant to work with the other question since you never mentioned that. You simply said you want to move the LineRenderer when the object is moved so I this should do it. If you can't combine the two,you may want to create a new question with what you have tried so far and what exactly you expect to happen and I will see if that's something I can help with – Programmer Nov 09 '17 at 19:39
0

The problem is in your assumption of how the line renderer works.

Both Vector3 objects that you posted are not points, but directions instead.

Have a look at the difference between Debug.DrawLine(), which is how you assume the line renderer works, and Debug.DrawRay() which is how it actually works.

Julian Declercq
  • 1,536
  • 3
  • 17
  • 32