x


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.

more ▼

asked Oct 06 '11 at 07:35 PM

stromdotcom gravatar image

stromdotcom
1 1 1 3

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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;
more ▼

answered Oct 06 '11 at 10:58 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

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?

Oct 06 '11 at 11:27 PM stromdotcom

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)

Oct 07 '11 at 12:22 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Oct 06 '11 at 10:46 PM

Tseng gravatar image

Tseng
1.5k 6 8 23

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jan 21 at 04:42 AM

carlos.castaneda gravatar image

carlos.castaneda
16

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x212
x31
x20

asked: Oct 06 '11 at 07:35 PM

Seen: 5125 times

Last Updated: Jan 21 at 04:43 AM