# Corner Screen Coordinates to World Coordinates

Hello friends. Trying to dynamically generate meshes based upon screen size, but can’t find a decent way to convert the corners of the screen to world coordinates. I’ve tried both ScreenToWorldPoint and ViewportToWorldPoint from the Camera class. Here’s my code:

``````camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
camera.transform.position = new Vector3(0, 0, 0);
float length = camera.ScreenToWorldPoint(new Vector3(0, 0, Screen.height)).z - camera.ScreenToWorldPoint(new Vector3(0,0,0)).z;
float width = camera.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0)).x - camera.ScreenToWorldPoint(new Vector3(0, 0, 0)).x;
``````

When I print out width or length, I get a negative number for one of them, and a zero for the other. Obviously, I’m not doing it right. Can someone show me the correct way?

Hi KidNova,

The parameter in ScreenToWorldPoint you are using is wrong. First one is x position on the screen (0 being left side of the screen), second if y position on the screen (0 being bottom side of the screen), third one is the distance to the plane containing the Camera position and perpendicular to the viewing direction.

For example : `camera.ScreenToWorldPoint(new Vector3(0, Screen.height, camera. nearClipPlane)` will return the position in the left top corner of the near clip plane.

If your camera is facing z direction, the height of the screen will be given by

``````camera.ScreenToWorldPoint(new Vector3(0, Screen.height, camera. nearClipPlane)).y - camera.ScreenToWorldPoint(new Vector3(0, 0, camera. nearClipPlane)).y
``````
1 Like

i used this for Orthographic camera, to make edgecolliders around screen:

Thanks, a lot zhoufei. Your solution helped me out a lot!

You might want the 8 vertices of the view frustum, which defines the volume visible on the screen. For that you need to pass both the near and far plane value of your camera as z of the screen corners to ScreenToWorldPoint.

Another viable option is to use ScreenPointToRay to raycast on each of the four corners of the screen and find out what it hits. I have one game where it’s relevant to raycast against the terrain in this fashion.