• 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 _nevermind · 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

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

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

Related Questions

Fitting a series Gameobjects within camera view 0 Answers

ScreenToWorldPoint not accurate 0 Answers

Hide camera view from top to bottom 0 Answers

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

How to get Directional Light Shadows to render through an Orthographic Camera? 1 Answer

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