Pixel Perfect plus Perspective Camera

I’m just moving from GM to Unity so I’m very noob. I’m developing a sidescrolling platformer. I understand orthographic cameras but I don’t want to use it as I want to be able to gently zoom in and out when certain things are happening and get that 3d effect. However the camera would normally be a set distance and I would like the player and all objects on the 0 plane to be pixel perfect. Additionally, I would love to know exactly how much to scale the background up to get pixel perfect size if for instance I had the background at a depth of 20.

So my basic question is how do figure out where to place the camera and how much to scale each layer by to get back to pixel perfect. And not in code, I understand how I would do that. I just want to get a good grasp on building the layers in the 3d view without just looking at the game view and thinking “yeah that looks about right”

Actually, I figured it out with triangles n math and stuff.

How exactly did you figure it out? Would be helpful if you could let us know?

@obscene : Could you give a quick explanation of how you did this? I’m also creating a 2D game using a Perspective camera, and am wondering how to calculate the appropriate Field of View so that everything at Z = 0 is pixel accurate.

Thanks!

Here’s some fiddly math to line up a perspective camera so that Z=0 is pixel perfect. This goes in addition to the setting the sprite filter to Point. The rest is trig.

var pixelMult = 3; // scaling factor, assumes 100ppu unity default, and scales up to my desired 3 pixel squares.

var camera = GetComponent<Camera>();
var camFrustWidthShouldBe = Screen.height / 100f;
var frustrumInnerAngles = (180f - camera.fieldOfView) / 2f * Mathf.PI / 180f;
var newCamDist = Mathf.Tan(frustrumInnerAngles) * (camFrustWidthShouldBe/2);
transform.position = new Vector3(0, 0, -newCamDist / pixelMult);

This is run once on Start for the camera. Let me know if I’ve been vague about anything (other than showing the work for the trig because I’m lazy)

1 Like

Sorry to bump this old thread, but could you explain what the pixelMult is in more detail? Is it the desired PPU or is it enlarging by a factor of three in this case? I’m trying to make a similar set up and found this thread.

From what I can tell from the comments, pixelMult is how many SCREEN pixels you want to represent one SPRITE pixel in your art.

1 Like