Draw line in 2D space using LineRender

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?

Here’s my thinking, you really do need a point in 3D space in the real world inside the scene.

Just define ("in your head’) a rectangle plane in your scene. So let’s say at z=5, and the rectangle runs from -4 to +4 and from -3 to +3.

(I suggest you actually put a green plane in the scene, where your imaginary recntangle is!)

now very simply, map the GLASS coordinates to that new imaginary rectangle. So for example .2,.9 would be -1,2.5 … (z remains as 5) … you see what I mean?

Again go directly from the glass to your Imaginary Friend rectangle. Viewport is not relevant - I think - in what you are trying to do.

But here’s an important point. Basically every video game has a flat collider that sits there for no other purpose than catching touches from the user.

Could the problem here be … are you hip to the physics “LAYERS” system? it’s critical and you have to use it all the time. It’s great and super-easy. (If you already know about this sorry, there are future ultra-oob readers to consider :slight_smile: )

Make a new layer called “only for touches” or “glass conversions” or whatever. Make a flat collider (a flat box is fine) and typically child it to the camera (if your camera never moves, say 2.5D, just sit it there)

And then exactly as you first describe, just cast on to that thingy for touches. Like I say, this is really common, most scenes have a “for getting touches” collider like this … perhaps it’s what you need here?

BTW you may enjoy this article-length answer !

and this

NOTE … also worth mentioning (again on this site it’s impossible to know if this is your first day with Unity or you are a seasoned pro, sorry - it may help someone in the future anyway). I mention 2.5D scenes above, indeed, are you hip to the orthographic camera? (Just click your camera, look in the inspector and click orthographic.) In a word, this is how you do things like 2.5D games, and other obscure uses, in Unity. conceivably this could be relevant to you. Look carefully at the camera frustrum (shown in white lines) in normal V. ortho. Hope it helps (someone) !!