- Home /
What is the relationship between camera size, units, mesh size and resolution?
When I'm working with an orthographic camera (doing 2D) I keep running into this issue where it would be great to know the math behind the relationship of the camera size and the units in unity space.
For example, if I know I'm in 1024x768 resolution, and my camera size is 1, and I want a mesh that completely fills up the screen exactly, how do I calculate it's dimensions?
And if I switch to 960x640 resolution, how do I know how to adjust the camera and resize the mesh to fit the new resolution exactly? What if the camera is now size 1.2? etc.
I know there has to be some math that should be nailed to my wall that I can use to do these calculations.
Answer by aldonaletto · Oct 06, 2011 at 10:58 PM
The height is 2 * size, and the width is height * aspect. You can calculate them with these instructions:
var height = 2*Camera.main.orthographicSize; var width = height*Camera.main.aspect;
Ok, this makes sense. And then everything just scales to the resolution I assume? So if I have resolution set to 1024x768, then I can assume that 2m in unity units = 768 pixels in pixel space? If so, will it always scale by height?
Yes, height is the "official" screen measure in Unity - height is directly related to the Size (or orthogonalSize), and width is related to height. Depending on what you need to do, maybe it's a better idea to use Camera.main.ScreenToWorldPoint to convert pixel to world coordinates. This function receives a parameter Vector3 where x and y are the pixel coordinates and z is the distance from the camera, and returns the corresponding point in the 3D world. The inverse function also exists, WorldToScreenPoint: it converts a 3D world point to pixel coordinates x and y (the z value returned is the distance from the camera)
Thank you aldonaletto! You saved me from a lot of frustrating headaches!
Thanks. Hopefully this translates to render to texture so I can calculate pixel from world space :)
Answer by Tseng · Oct 06, 2011 at 10:46 PM
The Orthographic size is the visible area from center of the screen (read: half height) to the top in unity units (1 unity unit = 1m).
So if you have a orthographic size of "1" it will show exactly 2 units height (2 meters). So if you have a standard cube (Menu GameObject > Create Other > Cube), then it will be exactly half the screen height, because the default is 1x1x1 units.
Answer by carlos.castaneda · Jan 21, 2013 at 04:42 AM
The solution isn't scale every object in your scene, in fact is "re-size" the projection of the camera according to a specific aspect-ratio, take a look this: for me is THE SOLUTION !!!
http://gamedesigntheory.blogspot.com/2010/09/controlling-aspect-ratio-in-unity.html
Your answer
Follow this Question
Related Questions
Measuring distance on Camera Screen-Space Canvas in -canvas units- 1 Answer
Make a value inverted/opposite. 0 Answers
Problem with mathf function 2 Answers
Bomb Trajectory for a "Water Canon" (Maths) 1 Answer
health bar position formula 3 Answers