• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by prank · Feb 21, 2013 at 07:20 PM · androidcamerascalescreensize

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Feb 21, 2013 at 09:28 PM 0
Share

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.

avatar image prank · Feb 22, 2013 at 04:31 PM 0
Share

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?

avatar image robertbu · Feb 22, 2013 at 04:37 PM 0
Share

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.

avatar image prank · Feb 22, 2013 at 06:14 PM 0
Share

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?

avatar image robertbu · Feb 23, 2013 at 06:09 AM 2
Share

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;
     }
 }
avatar image boynedmaster robertbu · Nov 17, 2017 at 02:27 PM 0
Share

i know this is from 4 years ago, but thanks loads robertbu, been stuck on this problem for probably 3 days now

Show more comments
avatar image
-1

Answer by arian-x · Mar 14, 2014 at 06:30 PM

you can use Camera.ScreenToWorldPoint() !

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image prank · Feb 23, 2013 at 01:50 PM 0
Share

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.

avatar image Owen-Reynolds · Feb 23, 2013 at 02:48 PM 0
Share

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.)

avatar image prank · Feb 23, 2013 at 03:10 PM 0
Share

Ok now it looks good in the game view on the computer but now it's very small on the phone.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set up resolution on Android? 1 Answer

Accessing camera torch/flash issues - Torch works but can't access while streaming webcamtexture 0 Answers

Mobile Device Video Camera 1 Answer

How to render GUI on a stereo scene, with Samsung Gear VR and Android? 1 Answer

Camera Size on Android 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges