Skip to content

Instantly share code, notes, and snippets.

@martinpi
Created March 4, 2019 15:39
Show Gist options
  • Save martinpi/cf94e9d2c34a8254016f7c470467d4bc to your computer and use it in GitHub Desktop.
Save martinpi/cf94e9d2c34a8254016f7c470467d4bc to your computer and use it in GitHub Desktop.
Use screen width instead of height as reference for scaling orthographic size of a Unity camera. Especially useful for supporting a wide range of mobile phones.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/* Camera usually uses screen height as their reference for orthographic size.
This script makes it scale with width instead.
*/
public class CorrectAspectRatio : MonoBehaviour {
public float targetWorldWidth = 14.5f;
public float minWidth = 12f;
void Start() {
float w = Screen.width;
float h = Screen.height;
Camera.main.orthographicSize = Mathf.Max(minWidth, h * targetWorldWidth / 2 / w);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment