# Viewport to World Coordinates

Hi guys, I’ve been looking for similar questions but I couldn’t find what I was looking for.

Lets say I have a scene like this.

And I have two gameobjects, the left black dot being at x position -10
While the right black dot being at X position 10.

I want the screen width to return approximately 40 in this case.
If i were to lower the screen width to here

I would want the screen width to return approximately 20.
I’ve seen the methods ViewportToWorldPoint and ScreenToWorldPoint
but I don’t think they can solve my problem here (i may be totally wrong).
Any suggestion guys?

you want to move your camera around so that the worldcoords visible on the blue plane are always between 20-40?

viewport dimensions are a flat float between 0 and 1 on the camera lens.

screenpoints are pixel based, 0-480, etc.

if you’re orthographic you could shoot a screenpointoray out from the left most point of the screen(in world coords) and see where in world coords it hits the blue plane, then do the same on the right (or not if you are always dead center) and adjust the FoV or use trigonometry to find the best location for the camera to be at (on the z axis)

if you’re perspective… you can calc the distance to the blue plane, and knowing your FoV and distance to either of the black dots figure out how far you should actually be back using trigonometry and lerp towards the intended number.

Someone else was able to answer on reddit
To clarify I wanted the X coordinates for the left and rightmost
visible pixels on the screen.

``````//lowerLeft.x will be the X coordinate of the leftmost visible pixel
Vector3 lowerLeft=camera.ViewportToWorldPoint(new Vector3(0,0,0));

//lowerRight.x will be the X coordinate of the rightmost visible pixel
Vector3 lowerRight=camera.ViewportToWorldPoint(new Vector3(1,0,0));

float screenWidthInWorld=(lowerLeft-lowerRight).magnitude;
``````

Only works for orthgraphic but that’s what I have.