Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting positions of a line renderer on moving and rotating a line

I have a line with line renderer attached to it . The user can move the line and rotate it. How do I go about getting the new positions of the line renderer which has been moved or rotated? since the coordinates of vertices of line renderer do not change , only the positions and the rotation of the line object as a whole changes .

ScreenShot of line object

The positions in the bottom part of image do not change on moving or rotating it. These positions are returned by the getpositions() method which is not useful in my case.

like image 214
Kitwradr Avatar asked Jul 23 '18 11:07

Kitwradr


People also ask

How do I move a line renderer?

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.

What is a line renderer?

Description. The line renderer is used to draw free-floating lines in 3D space. This class is a script interface for a line renderer component.


1 Answers

The LineRenderer in unity takes a list of points (stored as Vector3s) and draws a line through them. It does this in one of two ways.

  1. Local Space: (Default) All points are positioned relative to transform. So if your GameObject moves or rotates, the line would also move and rotate.

  2. World Space: (You would need to check the Use World Space Checkbox) The line will be rendered in a fixed position in the world that exactly matched the Positions in the list. If the gameObject moves or rotates, the line would be unchanged

So what you really want to know is "How do I get the world space position of a local space point in my line?"

This common use case is addressed by methods on a gameObjects transform

Transform.TransformPoint

It takes a local space point (which is how the data is stored in the line renderer by default) and transforms it to world space.

An Example:

using UnityEngine;
using System.Collections;

public class LineRendererToWorldSpace : MonoBehaviour
{
    private LineRenderer lr;

    void Start()
    {
        lr = GetComponent<LineRenderer>();

        // Set some positions in the line renderer which are interpreted as local space
        // These are what you would see in the inspector in Unity's UI
        Vector3[] positions = new Vector3[3];
        positions[0] = new Vector3(-2.0f, -2.0f, 0.0f);
        positions[1] = new Vector3(0.0f, 2.0f, 0.0f);
        positions[2] = new Vector3(2.0f, -2.0f, 0.0f);
        lr.positionCount = positions.Length;
        lr.SetPositions(positions);
    }

    Vector3[] GetLinePointsInWorldSpace()
    {
        Vector3[] positions;
        //Get the positions which are shown in the inspector 
        var numberOfPositions = lr.GetPositions(positions);
        //Iterate through all points, and transform them to world space
        for(var i = 0; i < numberOfPositions; i += 1)
        {
            positions[i] = transform.TransformPoint(positions[i]);
        }

        //the points returned are in world space
        return positions;
    }
}

This code is just for demonstration purposes, as I am not exactly sure of the use case. Also, my links are to 2018.2 which is a very recent version of unity, however the logic and methods used should be quite similar going back.

like image 178
Reese Jones Avatar answered Nov 02 '22 04:11

Reese Jones