Reputation: 380
I am trying to draw a line with LineRenderer that follows the user's finger movements. So far I have been unable to draw a line. This is the code i got.
how do i do it?
var c1 : Color = Color.white;
var c2 : Color = Color.white;
var line : GameObject;
var lengthOfLineRenderer : int = 5;
function Update () {
var touchCount : int = 0;
if (Input.GetMouseButtonDown (0)) {
touchCount++;
}
if (Input.touchCount == 1) {
if (Input.GetTouch(0).phase == TouchPhase.Moved) {
var lineRenderer : LineRenderer = line.AddComponent(LineRenderer);
lineRenderer.SetColors(c1, c2);
lineRenderer.SetWidth(0.2,0.2);
lineRenderer.SetVertexCount(lengthOfLineRenderer);
lineRenderer.SetPosition(0, gameObject.transform.position);
}
}
}
Upvotes: 0
Views: 1968
Reputation: 10721
The idea would be to check the displacement of the input position. If no movement, or too small delta, then wait, if the movement is large enough than you add a vertex and reset all positions:
This is pseudo-code:
Vector3 previous;
int vertexCount = 0;
List<Vector3>positions;
void Update(){
if(Input.detected){
Vector3 current = Input.position;
if(Vector3.Distance(previous, current) < threshold){ return; }
SetVertexCount(++vertexCount);
positions.Add(current);
for (int i= 0; i < vertexCount; i++)
{
lineRenderer.SetPosition(i, positions[i].transform.position);
}
}
}
The list of positions will keep all previous positions to be passed to the line renderer.
The whole input section is only pseudo code and should be turned into Desktop or mobile input.
Upvotes: 1