- Home /
How do I lock the width on an orthographic camera?
I'm trying to achieve this:
iphone5 ipad
The problem is that when the resolution changes, it is locking the height and expanding the width to the new ratio.
I have seen this solution:
float aspectRatio = (float)Screen.width / (float)Screen.height;
float cameraHeight = WORLD_WIDTH / aspectRatio;
camera.orthographicSize = cameraHeight/2f;
The problem with Screen.width does not work in the Editor. It gives me the size of the entire editor window and has nothing to do with the resolution set in the game window.
I would like to be able to preview what the game will look like at different resolutions and not just hope things look fine on all devices.
Oddly, the CanvasScaler can already do this. Does anyone know a way to achieve this so that I can preview different resolutions in the editor? Is there a way to get the world
this is what I ended up with:
int cameraHeight = 178;
int fixedWidth = 1136;
float desiredAspect = 16f/9f;
float aspect = Camera.main.aspect;
float ratio = desiredAspect / aspect;
Camera.main.orthographicSize = cameraHeight * ratio;
cameraHeight was just the height that I happened to be working in. Seems very hacky. Is there a better way?
Answer by omgdave · Jul 20, 2015 at 06:27 AM
the above script seems to handle it just fine. I put it in a call that is run once outside the editor and run constantly in the editor. (fixedWidth was not actually needed). My camera height happened to be really large before. I made it 11.36 because all the sprite stuff was 100 pixels to units by default, and i figured I should just divide 1136 by 100 and I'd hopefully be pixel perfect. If there are better ways to do this then let me know.
In a side note, using Canvas and it's auto-scaling solutions did not solve the problem and had a noticeable performance difference.
int cameraHeight = 11.36;
float desiredAspect = 16f/9f;
float aspect = Camera.main.aspect;
float ratio = desiredAspect / aspect;
Camera.main.orthographicSize = cameraHeight * ratio;
Oh my God, I've been fighting this all day! Thank you! Just for future visits, the way to get the cameraHeight variable, is: referenceHeight / 2 / pixelsPerUnit, where referenceHeight is the height of your base-resolution. I decided to make my base-resolution 720p (1280x720), so my referenceHeight is 720.
Your answer
Follow this Question
Related Questions
Resizing orthographic camera to fit 2d sprite on screen 1 Answer
How can i prevent the camera from goin down the y axis (othographic Doodle jump camera) 1 Answer
Change Editor Camera Orthographic Size 2 Answers
Lerping orthographic camera size jitters 1 Answer
Why is the camera aspect ratio broken for the game tab? 0 Answers