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 omgdave · Jun 29, 2015 at 09:46 AM · cameraorthographiclockwidth

How do I lock the width on an orthographic camera?

I'm trying to achieve this:

iphone5 alt text ipad alt text

The problem is that when the resolution changes, it is locking the height and expanding the width to the new ratio.

I have seen this solution:

   float aspectRatio = (float)Screen.width / (float)Screen.height;
   float cameraHeight = WORLD_WIDTH / aspectRatio;
   camera.orthographicSize = cameraHeight/2f;

The problem with Screen.width does not work in the Editor. It gives me the size of the entire editor window and has nothing to do with the resolution set in the game window.

I would like to be able to preview what the game will look like at different resolutions and not just hope things look fine on all devices.

Oddly, the CanvasScaler can already do this. Does anyone know a way to achieve this so that I can preview different resolutions in the editor? Is there a way to get the world

this is what I ended up with:

     int cameraHeight = 178;
     int fixedWidth = 1136;
     float desiredAspect = 16f/9f;
     float aspect = Camera.main.aspect;
     float ratio =  desiredAspect / aspect;
     Camera.main.orthographicSize = cameraHeight * ratio;

cameraHeight was just the height that I happened to be working in. Seems very hacky. Is there a better way?

ipad.png (18.2 kB)
iphone5.png (18.1 kB)
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

1 Reply

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

Answer by omgdave · Jul 20, 2015 at 06:27 AM

the above script seems to handle it just fine. I put it in a call that is run once outside the editor and run constantly in the editor. (fixedWidth was not actually needed). My camera height happened to be really large before. I made it 11.36 because all the sprite stuff was 100 pixels to units by default, and i figured I should just divide 1136 by 100 and I'd hopefully be pixel perfect. If there are better ways to do this then let me know.

In a side note, using Canvas and it's auto-scaling solutions did not solve the problem and had a noticeable performance difference.

  int cameraHeight = 11.36;
  float desiredAspect = 16f/9f;
  float aspect = Camera.main.aspect;
  float ratio =  desiredAspect / aspect;
  Camera.main.orthographicSize = cameraHeight * ratio;
Comment
Add comment · Show 1 · 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 Ultroman · Dec 12, 2015 at 07:34 PM 0
Share

Oh my God, I've been fighting this all day! Thank you! Just for future visits, the way to get the cameraHeight variable, is: referenceHeight / 2 / pixelsPerUnit, where referenceHeight is the height of your base-resolution. I decided to make my base-resolution 720p (1280x720), so my referenceHeight is 720.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Resizing orthographic camera to fit 2d sprite on screen 1 Answer

How can i prevent the camera from goin down the y axis (othographic Doodle jump camera) 1 Answer

Change Editor Camera Orthographic Size 2 Answers

Lerping orthographic camera size jitters 1 Answer

Why is the camera aspect ratio broken for the game tab? 0 Answers


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