Constant World-Scale When Changing Screen Resolution

Ok, I feel like I’m missing something and that I should already know how to do this.

I’m making a 2D game and using a perspective camera that is always pointing down the z-axis. Fairly common I think.

Imagine the screen resolution of my game is set to 1920X1080 and my 2D world is rendered to the screen.

If I now select a different screen resolution, say 960X540 (half the size), my entire world is scaled to fit this new window. Everything in the world is now smaller so that I can still see the same amount of the world. This seems to be the default behavior of Unity.

What I want to happen when the resolution changes to the smaller size, is for the world itself NOT appear smaller but rather it just gets clipped more do to the smaller window.

To be clear, I’m not talking about dynamically changing the resolution at runtime. User would choose resolution at start of game, then see more or less of it depending on the resolution they chose.

What’s an easy way to implement this affect while using a perspective camera. Any ideas??

-Jeff
@jeffweber

You can access Screen.width and Screen.height anywhere in your project. In your camera’s Start() function, you could set the camera’s field of view value based on your screen width and height. You’ll also want to do this anytime the screen gets resized.

public Camera cam;
float defaultScreenHeight = 1080f;
float defaultFov = 60;

void Start()
{
    OnScreenResized();
}

void OnScreenResized()
{
    cam.fieldOfView = defaultFov * (Screen.height / defaultScreenHeight);
}

Perfect. Just what I was looking for. Thanks.