- Home /
[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);
}
}
}
}
You have to call lineRenderer.SetVertexCount to have the right number of nodes (the same number as in your array).
you really don't need to change the question title to "solved" in answers - just accepting the answer is good enough! ;)
Answer by Herman-Tulleken · Nov 01, 2010 at 09:31 AM
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.
i keep getting the error of the lineRenderer not able to read the position from my array.
do i call the screenPoint and worldPoint in the update function?
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.
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).
Your answer
Follow this Question
Related Questions
How to be able to trace shapes? 0 Answers
Click objects to draw a line between them 0 Answers
Why does my array of LineRenders only render one line? 1 Answer
Line Renderer Ending In the Center 0 Answers
How to draw a line with touch? 1 Answer