- Home /
Cutoff top and bottom of screen instead of sides when changing aspect ratio
I am making a game that constantly scrolls to one side. I have it set up to work with ratios from 9:16 to 3:4 (portrait only game). It works well with the 9:16 size, but when it is played with a 3:4 aspect ratio, the game is much easier because you can see what is coming sooner.
My thought is to cutoff the top and bottom of the scenes some instead of widening the scenes (default function of unity when I change screen ratio).
Is there any way to accomplish this? Preferably in the editor and not by using code. I would imagine this is possible though components but I can't figure it out.
I am open to any other suggestions of how to solve my problem of the game being easier in the different aspect ratios.
Thanks!
Answer by kbumpious · Aug 17, 2015 at 02:31 PM
//attach this to your main camera object
void Start () {
float WORLD_WIDTH = 12f; //***Change this value based on how wide your world is***
float aspectRatio = (float)Screen.width / (float)Screen.height;
float cameraHeight = WORLD_WIDTH / aspectRatio;
GetComponent<Camera>().orthographicSize = cameraHeight/2f;
}
If you prefer that your game view is never cropped at all, use this modification to kbumpious' answer:
void Awake()
{
float worldWidthInUnits = 40f; // or whatever your world size is
$$anonymous$$imumCameraSize = GetComponent<Camera>().orthographicSize; // Get the starting default camera size and treat it as the $$anonymous$$imum
float aspectRatio = (float)Screen.width / (float)Screen.height;
float cameraHeight = worldWidthUnits / aspectRatio;
if(cameraHeight/2f > GlobalVars.$$anonymous$$imumCameraSize){
GetComponent<Camera>().orthographicSize = cameraHeight/2f;
}
}
This script was tested for various aspect ratios, and uses a landscape mode as the initial starting point.
Your answer
Follow this Question
Related Questions
Object resolution 0 Answers
Game fills only one quarter of the screen, why? (Android) 1 Answer
How can I make the game Screen to fit resolution 2 Answers
Get GUI size 1 Answer
Importing texture, problem with size - width and height get changed 3 Answers