In 2D games in Unity 3D is normal to use orthographic camera.
With importing sprite to the game, there is a option „Pixels to units“. Default setting is 100 and that means that 100 pixels of sprite will be 1 unit in game. If it’s our value 100 and we want sprite to be perfectly aligned with some other sprite, our values of X and Y must be rounded to 2 decimal places (for example 1.56).
If we want that our sprites would be pixel perfect, we need to set on all sprites the same value of Pixels to units. Next step is to use this script:
using UnityEngine; using System.Collections; public class pixelPerfect : MonoBehaviour { public float textureSize = 100f; float unitsPerPixel; // Use this for initialization void Start () { unitsPerPixel = 1f / textureSize; Camera.main.orthographicSize = (Screen.height / 2f) * unitsPerPixel; } }
Add this script to your main camera. The size of orthographic camera is how many units is half of its height. That’s why we take half height of screen and multiply it with value of 1/pixels per units.
Do you know other ways how to make pixel perfect camera? Share it in the comment section below!