# Projection of viewport on plane

I am trying to determine the ray-projection of a screen position on a plane. The goal is to use this projection to make sure the camera's viewport stays inside a cube's scale (but that really isnt important).

I managed to determine the point where the ray touches the plane using line-plane intersection math. This is my code:

``````Ray ray = Camera.main.ViewportPointToRay(new Vector3(1,0,0)); // bottom right corner
float distance = Vector3.Dot((plane.position - ray.origin),plane.up)/Vector3.Dot(ray.direction, plane.up); // distance = (p0 - l0) . n / l*n

Vector3 test = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance)); // world point in bottom right corner, distance away from camera
Vector3 test2 = ray.origin + distance*ray.direction; // ray projection with distance

``````

In my opinion test should equal test2, right? But it isnt .. test2 is correct, test isnt. Why is this?

I believe this is the same issue I had a few days ago, and I eventually stumbled across the fact that adding (distance) in to a Screen-To-Ray calculation actually chooses from a sphere of points around the camera, rather than a plane like I thought it should.

All Screen and Viewport functions perform camera projection according to the camera projection settings. If you want an orthographic projection, you would need to use an orthographic camera. By default cameras are set to perspective projection which will therefore result in a curvature around the camera. It's not entirely as Vicenti describes about choosing points on a sphere.

The ScreenToWorldPoint and ViewportToWorldPoint don't work as you expect. The z component defines a plane in world space away from the camera and the x and y coordinates are then projected as a ray and the intersection point is returned.

For example, create a camera at 0,0,-10 and a plane at 0,0,0 with a rotation of 270,0,0. If you were to create a ray and intersect it as you have done, the intersection point returned would be 10.9,-5.8,0. If you were to create a screen point at the same viewport x and y point 1,0 with a z component of 10 (the distance from the camera to the plane we created along camera's forward, not the distance to the intersection point), the point will be the same.

``````//The plane is 10 units away on camera.forward, parallel to the camera's view plane.
Vector3 test = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, 10));

Ray ray = Camera.main.ViewportPointToRay(new Vector3(1,0,0)); // bottom right corner
float distance = Vector3.Dot((plane.position - ray.origin),plane.up)/Vector3.Dot(ray.direction, plane.up); // distance = (p0 - l0) . n / l*n
Vector3 test2 = ray.GetPoint(distance); // ray projection with distance

//test should = test2

``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.8837751,"math_prob":0.9765439,"size":2696,"snap":"2023-40-2023-50","text_gpt3_token_len":637,"char_repetition_ratio":0.15490341,"word_repetition_ratio":0.13333334,"special_character_ratio":0.24591988,"punctuation_ratio":0.16333938,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9616057,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-22T09:00:21Z\",\"WARC-Record-ID\":\"<urn:uuid:bb7e1173-4f1d-46f8-b673-e8b5de864bd5>\",\"Content-Length\":\"24657\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:35f4dd5c-3ac3-4429-87ad-5604f01e0eb2>\",\"WARC-Concurrent-To\":\"<urn:uuid:44eafd45-77e2-40ed-9e9a-adbfc6fdaf5a>\",\"WARC-IP-Address\":\"184.104.178.75\",\"WARC-Target-URI\":\"https://discussions.unity.com/t/projection-of-viewport-on-plane/7901\",\"WARC-Payload-Digest\":\"sha1:QH76R6PV4IGQU3QUX7BNC5BJOPBX6OYP\",\"WARC-Block-Digest\":\"sha1:OY6OKXJRHQSH4FVIDS3RCJXDWQSTTGYF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506339.10_warc_CC-MAIN-20230922070214-20230922100214-00270.warc.gz\"}"}