I’ve been asked a few times about how the Orthographic camera in Unity3D functions and how to achieve 1:1 pixel-perfect mapping for 2D games and you’ll be happy to know it’s relatively (joke right there) simple. The following assumes we are shooting for a 1920×1080 (1080p) output. This method works best when you know the target resolution (closed systems, consoles) and/or screw everyone, you will play this at 1080p or it will look weird. You can work some math to alleviate some pixel crawl with multiple resolutions, which I won’t show you since you’ll have an understanding of the Orthographic camera and how it relates to pixels per units in Unity3D. From there, you should be able to figure the rest out :)
The first thing we need to go over is Orthographic Camera Size. What does it mean? When you first set out to create your 2D game in Unity3D, you’ll notice the camera’s default Orthographic size is 5. Cool. So what the hell is 5? That magic number is the number of units from the center half of the screen to the top, or bottom. It’s the halfway point in your vertical viewport. Armed with this knowledge and a hefty does of multiplication tables we can say that five units multiplied by two halves is 10 (5×2). Our viewport is exactly 10 Unity3D units tall. It doesn’t matter if it’s 4:3, 16:9 or 16:10 or any other possible aspect ratio on any TV, monitor or device. It will always be 10 units tall.
We want 1:1 pixel perfect mapping for our console game at 1080p so we are going to make sure that our artwork is designed for that specific resolution. After that, we need to import our sprites and change the Pixels to Units measurement in our Inspector Pane for that specific sprite (and others).
What is Pixels to Units? Its simply the relation between a single pixel in your sprite to world space in Unity3D (Units). The default setting is 100. Armed with this knowledge and knowing our screen height is exactly 10 units, that means our default screen height is 1000 pixels in size for that sprite, on any viewing source. That’s not exactly 1080p, is it? We need to change that. Let’s start by changing that default pixels to units and setting it to 1 since our artwork was created for 1080p:
What this will do is convert 1 pixel from that sprite into 1 Unity3D Unit. This means if your sprite is 50px in height, in world space it is now 50 units in height. We need to fix this and we’re going to do this with the Orthographic Size in the Camera. We know we want 1080p. We know we are using 1:1 pixel mapping and we need exactly 1080 units in height. Since our Orthographic Size is exactly half of the screen’s height: 1080/2 = 540. We need to change the Size of the Orthographic view to 540:
Now we know that our screen is exactly 1080 units in height and our pixel mapping is 1:1 for every sprite we use we have now just achieved 1:1 pixel mapping to world space in Unity3D.
Likewise, if you are working on pixel art in your game and are aiming for 1080p as your default resolution but are importing tiny little sprites (as is our case with STRAFE), you can decrease the Pixels to Units of the sprite on import and adjust Orthographic Size accordingly. For STRAFE, our sprites were imported at 10 Pixels to Units and our Orthographic size was 54. 54×10 = 540. Ortho Size x Pixels to Units x 2 = your desired vertical resolution.
Easy as pie!