0
\$\begingroup\$

I'm attempting to create 90 degrees top-down scenes where I'm working with a 4k or larger camera. What I'm trying to do is drop a script on the main camera and have a debug line that updates the main camera in [Execute Always] mode to tell me how many world units are visible to the camera width and height. For example, when dealing with a 1920x1080 camera I need to ensure 53.1876 width and 29.8704 units height are visible. When I switch the main camera to 5760x3420 I want to be able to ensure there are exactly 159.5628 units width and 89.6112 units height visible. The debug will allow me to change the camera's x value to increase or decrease altitude from game terrain or surface objects to achieve the desired number of visible world units width and height. I have tried to use ScreenToWorldPoint and ViewPortToWorldPoint but haven't been able to achieve desired results.

I was able to produce the desired effect with an Orthographic camera easily since the relationship of the orthographic size exactly corresponds to how many world units will be width and height when the aspect ratio is known such as the code example.

  var height = 2*Camera.main.orthographicSize;
  var width = height*Camera.main.aspect;

So I knew if I wanted a heights of 29.8704 I just had to make the orthographic size 14.9352. I'm looking to replicate this with a perspective camera while understanding the differences in views.

\$\endgroup\$
  • 1
    \$\begingroup\$ The width and height of the visible rectangle depends on the depth of that rectangle. Since for a perspective camera, what we really have is a visible frustum that widens as it gets further from the camera. Do you have a specific depth where you want to apply this measurement, or is there a different way you'd like to phrase this query for the 3D perspective case? \$\endgroup\$ – DMGregory Jan 25 at 22:01
  • \$\begingroup\$ I think the best way to get depth would be to raycast straight from the camera out(down in this case since it will be pointed at the terrain or building) and get the distance to whatever it hits first. \$\endgroup\$ – CloudSp4rk Jan 26 at 0:34
  • \$\begingroup\$ This will give you a different width and height of there happens to be one close thing in the middle of the frame, regardless of how far back most of the other on-screen content is. Are you sure that's the behaviour you want? \$\endgroup\$ – DMGregory Jan 26 at 0:39
  • \$\begingroup\$ In this particular use case, yes. I know it seems odd. The other alternative would be to have it raycast to hit a plane on the "plane" layer and I can position the plane manually at the right height. Actually that may be better for flexibility. \$\endgroup\$ – CloudSp4rk Jan 26 at 1:27
  • \$\begingroup\$ @CloudSp4rk you don't need a raycast a all. The camera has functions to convert a screen point to world point, given the camera settings and the depth of the plane. docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html \$\endgroup\$ – Louis Garczynski Jan 26 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.