Hey guys I have a question pertaining to drawing a line in 2D space on a touch interface. I was able to do this pretty easily with raycasts and hit info, however this requires me to actually have a raycast hit in order to draw the line. Here is how I am doing that.
void Update () {
if (Input.touchCount == 1) {
Touch touch = Input.GetTouch(0);
ourLine.enabled = true;
if (touch.phase == TouchPhase.Began) {
if (pew (touch.position)) ourLine.SetPosition(0, hit.point);
}
if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) {
if (pew (touch.position)) ourLine.SetPosition(1, hit.point);
}
if (touch.phase == TouchPhase.Ended) ourLine.enabled = false;
}
}
So I wanted to get the same kinda results without having to have a ray strike a surface. I figured the way to do this might be to try and use viewport coords to keep the line onscreen. Here’s what I tried, it “sort of” works in that a line is drawn but it is not in the correct location and appears really “thick”. I am having a hard time figuring out if I am not giving the correct vector values to set the lines or if my issue is with viewpoint coords.
void Update () {
if (Input.touchCount == 1) {
Touch touch = Input.GetTouch(0);
ourLine.enabled = true;
if (touch.phase == TouchPhase.Began) {
Vector3 startLine = Camera.main.ScreenToViewportPoint(touch.position);
ourLine.SetPosition(0, new Vector3(startLine.x, startLine.y, 2));
}
if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) {
Vector3 endLine = Camera.main.ScreenToViewportPoint(touch.position);
ourLine.SetPosition(1, new Vector3(endLine.x, endLine.y, 2));
}
if (touch.phase == TouchPhase.Ended) ourLine.enabled = false;
}
}
Any help would be most appreciated. Is there another/better way to do this?