- Home /

# 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?

**Answer** by Loius
·
Sep 28, 2010 at 02:58 PM

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.

**Answer** by skovacs1
·
Sep 28, 2010 at 04:49 PM

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

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Ray direction, projection matrix 0 Answers

how to calculate vector perpendicular to another and apply this to object direction? 2 Answers

How does ray snapping in the editor work? 3 Answers

Angles from Directions 2 Answers

Locate a game object using rays 1 Answer