- Home /
Fit an orthographic to touch edges of object.
I'm using the following code to scale the orthographic to fill the width of the play area across various device resolutions.
Vector2 screenSize = new Vector2((float)Screen.width, (float)Screen.height);
//playAreaSize is 3840, 2160, 1.
Vector3 playAreaSize = MasterManager.GameManager.Configuration.PlayArea.Size;
float result = playAreaSize.x * (screenSize.y / screenSize.x) * 0.5f;
Camera.main.orthographicSize = result;
A problem occurs on devices with standard definition aspect ratios (eg 4:3) where the orthographic camera does correctly size to the width, but the top reveals areas outside the play area (see image for example). I'd like to resize to the width unless doing so would put the camera view outside the play area, in which I'd want to use the height.
The 2880x1440 resizing is fine, as the camera view stays within the play area. The 1024x768 is bad as the camera view exceeds the edges of the play area.
untitled-1.png
(44.5 kB)
Comment
Best Answer
Answer by Punfish · May 24, 2018 at 04:27 PM
Resolved, it was so painfully obvious.
result = playAreaSize.y / 2f;