Get the screen diagonal length in world units

I’m trying to calculate the screen diagonal length in world units.
I’ve tried this but it doesn’t seem to work.

Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height)).magnitude

Any help would be appreciated.

Try:

Camera camera = Camera.main;

float magnitude = camera.ScreenToWorldPoint(new Vector3(camera.pixelWidth, camera.pixelHeight, camera.nearClipPlane)).magnitude;
1 Like

I think @GroZZleR 's answer depends on the camera position and will only be correct if the lower-left corner of the near-clip plane happens to be at the origin. Basically, you’re computing the distance from the origin to the upper-right corner of the near-clip plane.

To do it generally, you’d need two ScreenToWorldPoint calls to get the lower-left and upper-right corners and then take the magnitude of the difference between those points.

But I think it’s easier to do it with some trigonometry:

// For orthographic mode, it only depends on the orthographic size:
var height = camera.orthographicSize * 2f;
var width = height * camera.aspect;

// For perspective mode, it depends on the field of view and the desired distance from the camera:
var height = Mathf.Tan(0.5f * camera.fieldOfView * Mathf.Deg2Rad) * distance * 2f;
var width = height * camera.aspect;

// Then the diagonal is:
var diagonal = Mathf.Sqrt(height * height + width * width);
3 Likes

Thank you so much for this answer, I am using a 2d orthographic camera which at point (0, 0), so I just needed to multiply the value of the expression I posted by 2. Thank you for clearing this up.