5

You can rotate any visual Object in Unity with the Transform property. One exception is the LineRenderer. You can't move or rotate it with the transform property.

LineRenderer is moved with the SetPosition or SetPositions function so I managed to make it movable by the transform position property but I can't make make it rotate too.

Below is the code I am using to make it movable.

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 - diagLine.GetPosition(0);
    endPosOffset = transform.position - diagLine.GetPosition(1);
}

void Update()
{
    //Calculate new postion with offset
    Vector3 newBeginPos = transform.position + beginPosOffset;
    Vector3 newEndPos = transform.position + endPosOffset;

    //Apply new position with offset
    diagLine.SetPosition(0, newBeginPos);
    diagLine.SetPosition(1, newEndPos);
}

I tried to use the-same method I used to make it able to rotate but I am stuck in the first step of getting the offset because there is no way to access the rotation variable for LineRenderer but there is one for accessing the position GetPosition.

How can I get the LineRenderer rotation or how can I also make LineRenderer be rotated from the Transform property?

Below is an image that shows the behavior of LineRenderer with and without the script above. Position is now working with the script above enabled but rotation is not.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I am not familiar with Unity, but one would think something like `newBeginPos = transform.rotation * newBeginPos`? – Mike Mat Nov 11 '17 at 06:11
  • Nope. There is a`GetPosition` function I used to get the position now I am looking for `GetRotation` function which does not exist. As for your suggestion,it shouldn't work because you need the rotation of the LineRenderer and the GameObject it is attached to in order to get the offset for the rotation. Right now, you are just multiplying the position and rotation of this GameObject. I also tried that and it only makes the LineRenderer to disappear. – Programmer Nov 11 '17 at 06:50
  • Some way I can think of but may not work is having 2 empty gameobjects (children of the linerenderer), one in each end of the line renderer, rotate those using the transform rotation of the line renderer and then apply those gameobjects positions to the line renderer ends. – Daahrien Nov 11 '17 at 07:56
  • Good suggestion. I will give that a try. – Programmer Nov 11 '17 at 09:43

2 Answers2

5

You can use the transform's localToWorldMatrix:

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

void Update()
{
    //Calculate new postion 
    Vector3 newBeginPos = transform.localToWorldMatrix * new Vector4(beginPos.x, beginPos.y, beginPos.z, 1);
    Vector3 newEndPos   = transform.localToWorldMatrix * new Vector4(endPos.x, endPos.y, endPos.z, 1);

    //Apply new position
    diagLine.SetPosition(0, newBeginPos);
    diagLine.SetPosition(1, newEndPos);
}
Pluto
  • 3,911
  • 13
  • 21
  • This seems to be working and I know that `localToWorldMatrix` converts local to world space. Can you explain what exactly you did in your answer? – Programmer Nov 12 '17 at 04:46
  • 1
    @Programmer The matrix multiplication applies the transform's translation, rotation, scale to the vector (witch is exactly what you want). There is also `transform.TransformPoint(Vector3 position)` that does pretty much the same thing. Or you can set the linerenderer's Use World Space to false. – Pluto Nov 12 '17 at 12:00
3

You can set useWorldSpace attribute of LineRenderer to false, Then LineRenderer can be controlled using its transform component.

https://answers.unity.com/questions/39391/line-renderer-position.html

  • At least try to include a summary of the link. The answer should stand on its own. – mx0 Jul 01 '19 at 19:02