[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*);*
 *}*
*}*
*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);*
 *}*
 *}*
 *}*
*}*
*```*

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*);*
 *}*
*}*
*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*
 *}*
*}*
*```*
*<p>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).</p>*
*<p>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.</p>*