Changing Ortho Cam Size according to resolution

Ok this may be an easy question to answer, but I am terrible at math and need some help at this. So I am creating a 2d space shooter game in which the enemies and the player cannot go past the screen. The boundaries depend on the game’s current resolution, so if I change the resolution the player and the enemies may be able to go further accross the screen or may go less depending on the resolution setting. I have figured that changing the orthographic camera size according to the screen’s resolution would fix the problem, but I am having trouble getting the code down since there is a basic level of math that I need more understanding in. I.e simple division between aspect ratios… So if anyone could help with the code and perhaps add a link to some math tutorial vids/site/etc that would help in solving the problem, I would very much appreciate it :slight_smile:

public class CameraManager : MonoBehaviour
public float horizontalResolution = 1920;

	void OnGUI ()
		float currentAspect = (float) Screen.width / (float) Screen.height;
		Camera.main.orthographicSize = horizontalResolution / currentAspect / 200;

Here’s a decent overview that discusses pixel perfectness (no artifacts messing with your source art), how to manage the size of art assets on screen, and formula for determining orthographic camera size.

ortho cam tutorial

I wanted a reusable solution. Where you can just fill in the screen aspect ratio that you designed on and the starting size of your orthographic camera.

The GameObject the camera is focused on will always respect the minimum spacing to either the top or the side of the screen, depending on the current aspect ratio.

private void OnGUI()
        float aspectRatioDesign = (16f / 9f);
        float orthographicStartSize = 3.8f;
        float inverseAspectRatio = 1 / aspectRatioDesign;
        float currentAspectRatio = (float)Screen.width / (float)Screen.height;

        if (currentAspectRatio > aspectRatioDesign)
            currentAspectRatio -= (currentAspectRatio - aspectRatioDesign);
        } else if (currentAspectRatio < inverseAspectRatio)
            currentAspectRatio += (currentAspectRatio - inverseAspectRatio);

        Camera.main.orthographicSize = aspectRatioDesign * (orthographicStartSize / currentAspectRatio);

I hope that this will help someone… this script will change the camera (orthographic) size depending on the resolution. Add this script to the main camera.

bool MaintainWidth = true;
Vector3 CameraPos;
float DefaultWidth;
float DefaultHeight;

void Start()

    CameraPos = Camera.main.transform.position;
    DefaultWidth = Camera.main.orthographicSize *  the resolution the game was designed for;
    DefaultHeight = Camera.main.orthographicSize;

    if (MaintainWidth)
        Camera.main.orthographicSize = DefaultWidth / Camera.main.aspect;

    Camera.main.transform.position = new Vector3(CameraPos.x, -1 * (DefaultHeight - Camera.main.orthographicSize), CameraPos.z);


how it works for perspective camera?