• 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
1
Question by $$anonymous$$ · Mar 24, 2014 at 12:47 AM · cameraorthographic

Orthographic Camera - Same Width - Different Height

Same orthographic camera width on every iOS device but less height on iPhone and more height on iPad. How can I do this?

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
7

Answer by robertbu · Mar 24, 2014 at 03:18 AM

The camera's orthographic size is 1/2 of the vertical world units seen by the camera. So currently as you move the app across different devices the height remain the same but the width varies. From your question, I'm getting that you want to reverse that process.

Not all iPhones are the same resolution. A 5c is different than a 4s for example. So step #1 is to pick a specific resolution/phone to author for. Setup your orthographic size of the camera so that your game looks right for that phone.

Step #2 is to figure out the half width on the specific device. You can do that calculation by:

  orthoWidth = orthographicSize / screenHeight * screenWidth;

For example if your orghrathicSize is 6.0 and you author for the iPhone 5c, then:

 width = 6.0 / 640.0 * 1136.0 = 10.65.

Step #3: so on your target device, your half width is 10.65. Now in your app, you can do:

 function Start() {
      Camera.main.orthographicSize = 10.65 / Screen.width * Screen.height;
 }

This will adjust the camera so that on all devices it sees 10.65 half width.

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 digzou · Dec 25, 2014 at 01:00 PM 0
Share

Thanks a lot, was fed up figuring it out for 2 days! :D

avatar image
1

Answer by mancasal · Mar 18, 2016 at 03:24 PM

Put this script in the camera object.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Camera))]
 public class FixedWidthCamera : MonoBehaviour
 {
     public     float     screenWidth = 600;
 
     private Camera    camera;
 
     private float    size;
     private float    ratio;
     private    float     screenHeight;
 
     void Awake()
     {
         camera                     = GetComponent<Camera> ();
         ratio                     = (float)Screen.height / (float)Screen.width;
         screenHeight             =  screenWidth * ratio;
         size                     = screenHeight / 200;
         camera.orthographicSize = size;
     }
 
     void Update ()
     {
         camera                     = GetComponent<Camera> ();
         ratio                     = (float)Screen.height / (float)Screen.width;
         screenHeight             =  screenWidth * ratio;
         size                     = screenHeight / 200;
         camera.orthographicSize = size;
     }
 }
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
1

Answer by halken · Sep 17, 2017 at 07:24 AM

I had the same problem as the original poster, but these other solutions did not work for me.

To get a fixed width and variable height, I now use this in my camera script:

     // halfWidth = (# of World Units you want for the fixed width screen size) / 2
     public float halfWidth = 7f;
 
     void Start () 
     {
         // Force fixed width
         Camera.main.orthographicSize = halfWidth / Camera.main.aspect;

         // Optional: repositions the camera's Viewport to start at (0, 0) in the bottom left
         Camera.main.transform.position = new Vector3(halfWidth, Camera.main.orthographicSize, -10);
     }

Another useful tip is to draw the Rect of the Camera's Viewport in the editor. In my case, I also want the Editor's wire frame to start at (0,0).

     public float targetAspect = 9f / 16f;
 
     void OnDrawGizmos()
     {
         float widthSeen = Camera.main.orthographicSize * 2.0f * targetAspect;
 
         Gizmos.color = Color.cyan;
         // Optional: reposition the center of the wife frame as I did, or just use Camera.main.transform.position for the first parameter.
         Gizmos.DrawWireCube(new Vector3(halfWidth, halfWidth / Camera.main.aspect, -10), new Vector3(widthSeen, (widthSeen / Camera.main.aspect), 0));
     }


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 HappyTurtles · Aug 18, 2018 at 07:55 PM 0
Share

Thank you! That is exactly what I needed.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Lerping orthographic camera size jitters 1 Answer

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

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

Orthographic Camera too Big in 2D Game 0 Answers

camera width zoom 2 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