- Home /
Make unity camera appear all screen width
Hi all, When you design on unity (for android), it don't care about the screen width. It show any thing appear on the screen height of camera. But I want it base on the screen width, not screen height.I want to show anything that appear on the screen width of camera. Any solution for it. Thanks.
Answer by BeardOfFury · Nov 14, 2013 at 12:08 AM
For this you need to define what width you want your world to be (Let's say 800 units long). After that you need to get the current aspect ratio of the camera. Calculate the camera height based on the world width and aspect ratio of the camera, then apply to the orthographic size.
float aspectRatio = (float)Screen.width / (float)Screen.height;
float cameraHeight = WORLD_WIDTH / aspectRatio;
camera.orthographicSize = cameraHeight/2f;
The reason we use half the desired camera height for the orthographicSize is because that's what the orthographicSize actually is:
This should leave everything centered and lock the camera to your world width while different devices show more or less vertical height. If you want your camera to always be at the bottom of the level you'll need to adjust the camera's vertical position.
Hope this helps.
I don't understand the WORLD_WIDTH.But when I make WORLD_WIDTH=20, it work. (I have tested on device with resolution 1024*768)
WORLD_WIDTH was only meant to represent whatever you wanted WORLD_WIDTH to be (in your case 20) So that works for you now?
I have just tested only one device. I will test more with another resolutions. By the way, how can I calculate the WORLD_WIDTH size?
World width can be whatever you want it to be. It's literally just the horizontal size of the world you want to define.