x


[Solved]Need help on using Line Renderer.

I'm able to get the Line Renderer working now. Just want to share with others for those who may be facing the same problem that I had. Managed to get it done thanks to Herman.

Here's my code:

public var currentCamera : Camera;

var distance = 20;
var drawArray : ArrayList = new ArrayList();
var lineMat : Material;


function Start()
{
    var lineRenderer : LineRenderer = gameObject.AddComponent(LineRenderer);
}

function Update()
{
    ProcessInput();

    var lineRenderer : LineRenderer = GetComponent(LineRenderer);
    lineRenderer.SetVertexCount(drawArray.Count);

    for (var i : int = 0; i < drawArray.Count; i++)
    {
        lineRenderer.material = lineMat;
        lineRenderer.SetWidth(3,3);


        lineRenderer.SetPosition(i, drawArray[i]);
    }
}


function ProcessInput()
{

    for(var p : int = 0; p < Input.touchCount; p++)
    {
        var touchPoint : Vector3 = Input.GetTouch(p).position;
        var screenPoint : Vector3 = new Vector3(touchPoint.x, touchPoint.y, distance);
        var worldPoint : Vector3 = currentCamera.ScreenToWorldPoint(screenPoint);

        if(Input.touchCount == 1)
        {
            if(Input.GetTouch(p).phase == TouchPhase.Began)
            {
                if(drawArray.Count == 0)
                {
                    drawArray.Add(worldPoint);
                }

                else
                {
                    drawArray.Clear();
                }
            }

            if(Input.GetTouch(p).phase == TouchPhase.Moved)
            {
                drawArray.Add(worldPoint);
            }


            if(Input.GetTouch(p).phase == TouchPhase.Ended)
            {
                drawArray.Add(worldPoint);
            }

        }



    }
}
more ▼

asked Nov 01 '10 at 03:03 AM

Robindoo gravatar image

Robindoo
221 15 15 26

You have to call lineRenderer.SetVertexCount to have the right number of nodes (the same number as in your array).

Nov 02 '10 at 08:34 AM Herman Tulleken

(Call it before any call to SetPosition).

Nov 02 '10 at 08:34 AM Herman Tulleken

you really don't need to change the question title to "solved" in answers - just accepting the answer is good enough! ;)

Feb 01 '11 at 08:23 PM Cawas
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The magic function you are looking for is ScreenToWorldPoint. It is a method of Camera. So to use it, put a reference to a Camera in the script where you want to make the call - a public field, like this:

...

public Camera currentCamera;

Then construct a new Vector3. The first two coordinates are the xy coordinates that you get from touch. The third coordinate is the distance from your camera.

So

Vector3 screenPoint = new Vector3(touchPoint.x, touchPoint.y, distance);
Vector3 worldPoint = currentCamera.ScreenToWorldPoint(screenPoint);

Now you have a point in 3D space that you can use to construct the curve.

As in the GUILabel solution, it is easiest to keep track of these points in an array. (Especially if you want to interpolate or simplify the curve).

The last thing is then to synch up your LineRenderer positions with those in the array. The slow solution is to update the positions in every Update, like this:

public Update()
{
  ProcessInput();

  LineRenderer lineRenderer = GetComponent<LineRenderer>();
  lineRenderer.SetVertexCount(curve.Count);

  for(int i = 0; i < curve.Count; i++)
  {
    lineRenderer.SetPotision(i, curve[i]);
  }
}

ProcessInput()
{
   for each touch:
   {
     Vector3 touchPoint = touch.position;
     Vector3 screenPoint = new Vector3(touchPoint.x, touchPoint.y, distance);
     Vector3 worldPoint = currentCamera.ScreenToWorldPoint(screenPoint);

     //touch start  //do a check for this, clear curve list, add a point

     //touch continues //do a check for this, add point

     //touch ends //do a check for this, add last point

   }
}

Get this working first, then it should be easy to update the line renderer positions only as needed, and filter points (you do not need to add each and every touch point to this curve).

I am actually working on a something with similar functionality, and am experiencing some lag with the line renderer method as well (although in my case, there might be other issues), so it might be some work to get it nice and responsive.

more ▼

answered Nov 01 '10 at 09:31 AM

Herman Tulleken gravatar image

Herman Tulleken
1.6k 25 36 56

i keep getting the error of the lineRenderer not able to read the position from my array.

Nov 02 '10 at 07:41 AM Robindoo

do i call the screenPoint and worldPoint in the update function?

Nov 03 '10 at 02:31 AM Robindoo

Yup, see the modified code.

Nov 03 '10 at 06:21 AM Herman Tulleken

Thanks once again. It really works. Now i have a better understanding over LineRenderer. Just need to tweak it so it wont fade off so fast.

Nov 04 '10 at 03:13 AM Robindoo

No prob. BTW, if you come across a lag issue, I would be very interested in a solution (check out my question here: http://answers.unity3d.com/questions/26037/how-do-i-prevent-deal-with-input-lag-on-a-touch-interface).

Nov 04 '10 at 05:48 AM Herman Tulleken
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1999
x1356
x106
x101

asked: Nov 01 '10 at 03:03 AM

Seen: 3936 times

Last Updated: Nov 04 '10 at 03:27 AM