[SOLVED] Is there a way to determine the screen width in units?

I was wondering if there is a way of determining the screen in units.

I figured Screne.widthInUnits or something would be available but searching the docs found nothing.

Asked professor Google but his responses were not very enlightening.

Thank you,
John Cordeiro

are you looking to guage how many units are visible to the camera?

if it is, it would depend on whether your viewing frustum is orthographic or perspective to size what you are looking at.

ScreenToWorldPoint() might be worth a look. coupled with screen.width and height
https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

this takes a vector with an X and Y value in screenspace ( pixel width and height where 0, 0 is the lower left) and a float for the distance of the near clipping plane. so doing it twice will give you lower left and upper right vector3 values which you can subtract to get width and height in world units.

in a camera with orthographic projection there is no perspective so the shape of the viewing frustum never changes. so the clip plane distance is arbitrary.

but, with a perspective view, like say in command and conquer, if you want to find the viewable width of the plane at say ground level, you would use the ScreenToWorldPoint with the clip plane distance of the ground from the camera for the lowerleft and upper right, then you can use them with a little subraction to get the width and height in units for that specific viewing plane.

theres probably a more elegant way of doing it, but I cant think of one just now :frowning:

2 Likes

The magic incantation is something like:

For the orthographic camera, Camera.main.aspectRatio * Camera.main.orthographicSize * 2.

The field names might not be spelled exactly like that. In any case, the orthographic size is half the height, and the aspect ratio is width/height, so this formula would give the width in game units.

1 Like

I do not know built-in functions for that (I see that other replies have suggested some, you should probably test that out). I just defined static constants, because I know how large my game space is.

thanks to Mr. OboShape , I know ScreenToWorldPoint() .
and I wrote some code. see below.

1 Like