Understanding Orthographic SizeHow to get the perfect device zoom in Unity

Author Waldo
Published June 6, 2018

In this video I explain what Orthographic Size is in Unity and how to calculate it for your game so that you have the perfect device zoom. I also show examples of how to adjust Orthographic size in your camera script so that you have the perfect zoom every time a scene is loaded, despite whatever device is being used.

Video Walkthrough

  • 0:40 Problems with Orthographic Size
  • 1:10 How to Calculate Orthographic Size
  • 2:20 Creating a C# Script to Calculate OrthoSize
  • 4:15 Calculating for Horizontal Orientation
  • 4:50 Perfect Fit Zoom

Source Code For Vertical Fit

Use this code for when you want the the left and right edges of the area to touch the camera edges.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraScript : MonoBehaviour {
    public SpriteRenderer rink;

	// Use this for initialization
	void Start () {
        Camera.main.orthographicSize = rink.bounds.size.x * Screen.height / Screen.width * 0.5f;
	}
}

Source Code For Horizontal Fit

Use this code for when you want the top and bottom edges of the area to touch the camera edges.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraScript : MonoBehaviour {
    public SpriteRenderer rink;

	// Use this for initialization
	void Start () {
        Camera.main.orthographicSize = rink.bounds.size.y / 2;
	}
}

Source Code For Entire Fit

Use this code when you want to calculate so the entire area is in view.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraScript : MonoBehaviour {
    public SpriteRenderer rink;

	// Use this for initialization
	void Start () {
        float screenRatio = (float)Screen.width / (float)Screen.height;
        float targetRatio = rink.bounds.size.x / rink.bounds.size.y;

        if(screenRatio >= targetRatio){
            Camera.main.orthographicSize = rink.bounds.size.y / 2;
        }else{
            float differenceInSize = targetRatio / screenRatio;
            Camera.main.orthographicSize = rink.bounds.size.y / 2 * differenceInSize;
        }
	}
}

This tutorial is sponsored by this community

In order to stick to our mission of keeping education free, our videos and the content of this website rely on the support of this community. If you have found value in anything we provide, and if you are able to, please consider contributing to our Patreon. If you can’t afford to financially support us, please be sure to like, comment and share our content — it is equally as important.

Join The Community

Discussion

Browse Tutorials About