Understanding Orthographic SizeHow to get the perfect device zoom in Unity

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.

In this video, I discuss:

  • 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;
        }
	}
}

LINK TO THIS POST

SHARE THIS POST


DISCUSSION