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?

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.

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;
	}
}

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));
    }