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.
1 Answer
1For 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)
– minhthinhWORLD_WIDTH was only meant to represent whatever you wanted WORLD_WIDTH to be (in your case 20) So that works for you now?
– BeardOfFuryI have just tested only one device. I will test more with another resolutions. By the way, how can I calculate the WORLD_WIDTH size?
– minhthinhWorld width can be whatever you want it to be. It's literally just the horizontal size of the world you want to define.
– BeardOfFury
What is the platform you are developping for?
– PhilRousseI'm making a game for Android(tablet).
– minhthinhAre you using an orthographic camera?
– BeardOfFuryYes, I'm using orthographic camera.
– minhthinh