Skip to content

Instantly share code, notes, and snippets.

@drcharris
Created March 22, 2018 14:40
Show Gist options
  • Save drcharris/fef50007791ed9d3a24a4cfe302df46d to your computer and use it in GitHub Desktop.
Save drcharris/fef50007791ed9d3a24a4cfe302df46d to your computer and use it in GitHub Desktop.
Adjust Cinemachine camera so that your screen width always matches a specific Unity units width.
using Cinemachine;
using UnityEngine;
/**
* Force the Cinemachine camera to make viewport exactly as wide as the bounding object.
* This allows us to always be 100% wide regardless of device. Handy for vertical scrollers.
*/
public class FWCam : MonoBehaviour {
public int fullWidthUnits = 14;
void Start () {
// Force fixed width
float ratio = (float)Screen.height / (float)Screen.width;
GetComponent<CinemachineVirtualCamera>().m_Lens.OrthographicSize = (float)fullWidthUnits * ratio / 2.0f;
}
}
@Matthew-J-Spencer
Copy link

This works great, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment