Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by zefrof · Nov 19, 2013 at 03:27 PM · c#camerascenescreen resolution

Keeping an object in camera view in multiple screen resolutions?

I am making an arcade shooter, and I want the player to always be in a certain area. At one screen resolution, the player maxes out at 14 and -14 for left and right, but in another resolution it's 12 and -12. How can I keep the player in the camera area even though the max values change on different screen resolutions? I program in C# normal so I'd prefer C# code if possible, but I can do JavaScript if need be.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tomer-Barkan · Nov 19, 2013 at 06:55 PM

The way it works, is that the camera "size" that you set (field of view for perspective, size for orthographic), defines the distance from top to bottom. But then, when the aspect ratio changes (as in 4:3, 16:9, 16:10, etc - this is not the same as resolution), the height of your camera's view remains the same, but since the ratio between height and width changed due to the new aspect ratio, the width of the camera view must change as well.

So say the height is 30 game units, and the aspect ratio is 4:3 (say 800x600) - then the width is 4/3 times the height, so the width is 40.

Now if you change the aspect ratio to 16:9 (say 1920x1080) - then the width will be 16/9 times the height = around 53.

You see, now in the widescreen aspect ratio, you will see more to the sides.

How exactly you handle this, is up to you. You can decide you always plan for the lowest aspect ratio (5:4) - then you design the game using this aspect ratio, and any other resolution will have MORE to the sides, instead of left. So you don't have to be afraid of parts of the game being cut out.

A better but harder way to deal with this would be to plan a different camera view for each of the common resolutions. This requires for you to design more than one GUI, and then set the camera's values and the GameObject's locations according to the resolution.

If you are an indie developer with limited resources I recommend going for a constant view. Plan for 5:4 aspect ratio, and if someone has a wider screen they will get more in the sides, but everything that you plan to be in the screen will always be in the screen.

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 Sparrowfc · Nov 19, 2013 at 04:07 PM

Usually I made a static class to handle resolution adaption. I'm assuming you talking about a 2D UI object but not a 3D object. Hopes that may help

 public class ResAdaptor
 {
     static float readonly originWidth = 1024;
     static float readonly originHeight = 768;
     static float xRate = 0;
     static float yRate = 0;
 
     public static float XRate
     {
        get
        {
           if(xRate == 0)
              xRate = Screen.width / originWidth;
           return xRate;
        }
     }
 
     public static float YRate
     {
        get
        {
           if(yRate == 0)
              yRate = Screen.height/ originHeight;
           return yRate;
        }
     }
 
     public static Vector2 AVector2(float x, float y)
     {
         return new Vector2(x * XRate, y * YRate);
     }
  
     public static Rect ARect(float x, float y, float width, float height)
     {
         return new Rect(x * XRate, y * YRate, width * XRate, height * YRate);
     }
 }
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

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.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How to make the camera render RenderTexture if the camera is in another scene? 1 Answer

Multiple Cars not working 1 Answer

How to set the position of an object in another scene? 1 Answer

Camera Smooth Distance Change 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges