- Home /
Auto Scale camera depending on Screen Size - How??
I'm new on Unity and want to create a simple 2d game for Android. So I created a plane which fits my screen. Then I put the other object in front of the plane. Everything works fine on my device (720x1280|potrait) but when run it on a phone which has a lower resolution the objects get cut off.... So how can I set the camera to look at my plane. I want that my game scales everything to fit.
I'm searching on Google now for 2 weeks.. I didn't find a solution. But I found something about the orthografic size. I didn't understand it... I tried everything but nothing worked..
I hope someone can help me!! Thanks!
Answer by robertbu · Feb 21, 2013 at 07:52 PM
You can move the camera to a distance that will display everything. I don't know what is getting cutoff where but the general mapping between field of view of the camera is:
Height = 2 Tan(0.5 field_of_view) * distance;
And you can get the width use the ratio of Screen.width / Screen.height.
The last one, the Aspect ratio, you can set in the giant Game window tab. Forces your screen to be the same proportion as your phone, preventing cut-off surprises.
I tried to set the field of view that everything fits. But when I click on the play button. It resets the view. What's wrong?
Did you change the FOV on the camera object? Click on it in the hierarchy view and look at the value at runtime. If it is not the value you place there at edit time, then something in your scripts is resetting it. Note my suggestion above was to calculate the distance the camera needed to be at runtime and move the camera. If you have trouble translating the equation above into code, I can expand a bit.
I adjusted the FOV that everythings on the camera view. If I press on play the view gets reseted but it has the same value. It's strange.
Could you explain your calculation, please?
Given that your problem appears to happen in the editor, I suspect my calc will not be of help. There is something else going on. $$anonymous$$ake sure that your scripts are not changing the camera position or camera DOF.
Based on the calculations above, here is a bit of code. It is written expecting the camera to be looking towards positive Z and with the 2D surface for the game having a Z value of 0.0. You'll have to make appropriate changes if your setup is different. The public variable fWidth is the width in world coordinates you want the camera to see. If you don't know, you can either use the equation above to calculate the distance, or you can just place a couple of game objects in the scene and use their position to measure the distance.
Attach this to the camera:
public class PositionCamera : $$anonymous$$onoBehaviour {
public float fWidth = 9.0f; // Desired width
void Start () {
float fT = fWidth / Screen.width * Screen.height;
fT = fT / (2.0f * $$anonymous$$athf.Tan (0.5f * Camera.main.fieldOfView * $$anonymous$$athf.Deg2Rad));
Vector3 v3T = Camera.main.transform.position;
v3T.z = -fT;
transform.position = v3T;
}
}
i know this is from 4 years ago, but thanks loads robertbu, been stuck on this problem for probably 3 days now
Answer by Owen-Reynolds · Feb 23, 2013 at 03:11 AM
The only fix is to make the game in a screen with the same proportions as the target device. Otherwise it's like trying to show a widescreen movie on a round TV. There aren't any settings that can help.
The fix for movies is to put those black bars. Since many games have a free camera, Unity's "fix" is to just put on the screen whatever fits.
If your target device is 4 million by 3 million pixels, that's 4:3. So if you make your game with Game/Scene at 1200 by 900, it will look fine. Keeping your screen that size is a pain. So, Unity lets you click just under the Game tab and set the size (known as the Aspect Ratio.) That isn't really a setting -- it just helps your design "look the same" as it will on the target.
Ok that makes sense. Thanks! So you recommend me to use 900x1200? (portrait? Or am I wrong?
Sorry for that question. I'm new on Unity.
Look up the aspect ratio of the phone (compute it from the pixel dims.)
Once you find the dropDown that says 16:9, 4:3, 5:4 ... it will make a lot more sense (try resizing that window with anything except FreeAspect selected.)
Ok now it looks good in the game view on the computer but now it's very small on the phone.
Your answer
Follow this Question
Related Questions
How to set up resolution on Android? 1 Answer
Best Setting Size Orthographic Camera? 1 Answer
GameObject and Camera 2 Answers
Dragging Camera based on Touch 0 Answers
Android: Game not fitting on screen 1 Answer