- Home /
Question by
FreeReignPublishing · Jun 03, 2018 at 09:18 PM ·
c#touchline renderer
Problem solving. The line renderer does not follow touch as accurate as the mouse input. Not sure why?
I tested both mouse and touch and here's the code
public float width;
public Color color = Color.red;
private LineRenderer lr;
private Vector3[] linePoints = new Vector3[2];
public float speed;
private Vector3 lastpos;
private Vector3 fingerPos;
public bool mouseControl;
void Start()
{
lr = GetComponent<LineRenderer>();
if (!lr) lr = gameObject.AddComponent<LineRenderer>();
lr.material.color = color;
lr.widthMultiplier = width;
linePoints[0] = Vector3.zero;
lr.positionCount = linePoints.Length;
lastpos = transform.position;
fingerPos = transform.position;
}
void Update()
{
if (lr.enabled == false)
{
//mouse
if (mouseControl == true)
{
transform.position = Vector3.MoveTowards(transform.position, lastpos, speed * Time.deltaTime);
}
//touch
if (mouseControl == false)
{
transform.position = Vector3.MoveTowards(transform.position, fingerPos, speed * Time.deltaTime);
}
}
if (Input.GetMouseButton(0) || Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
if (lr.enabled == false) lr.enabled = true;
//mouse
if (mouseControl == true)
{
Camera c = Camera.main;
Vector3 p = c.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, c.nearClipPlane));
linePoints[1] = p;
lr.SetPositions(linePoints);
}
//touch
if (mouseControl == false)
{
Touch touch = Input.GetTouch(0);
Camera c = Camera.main;
Vector3 p = c.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, c.nearClipPlane));
linePoints[1] = p;
lr.SetPositions(linePoints);
}
}
else if (Input.GetMouseButtonUp(0) || Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
lr.enabled = false;
//mouse
if (mouseControl == true)
{
lastpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
lastpos.z = transform.position.z;
}
//touch
if (mouseControl == false)
{
fingerPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
fingerPos.z = transform.position.z;
}
}
}`
Comment
please format this, it's unreadable. use the 010101 button.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Reproducing the "spin" effect of Flick Kick Football 1 Answer
Turning touch to rotation 1 Answer