Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Orthographic camera width?

Discussion in 'General Graphics' started by bigSadFace, Feb 19, 2015.

  1. bigSadFace

    bigSadFace

    Joined:
    Aug 18, 2014
    Posts:
    116
    Hi all,

    I'm developing a game which uses an Orthographic camera for a 2D using 3D game. This was started as a prototype in Unity 3 before the 2D features were added. I've recently got around to finishing it.

    When I started the prototype it was just for a bit of fun to learn unity; however I've fleshed it out as a full game that works really well on touch devices.

    At the moment I have a border around the camera viewport which creates pillar-boxing at the sides and is used to keep objects in the scene. Is there a better way to handle this? I've tried determining the viewport width using the ortho size and aspect ratio to create a frame; however I can't get an accurate size.

    I've attached a few images to help illustrate the issue, the 16:9 aspect on the iPhone 5 causes the widest pillar-boxing. Interface and game elements have been stripped to make it clearer:

    Screen Shot 2015-02-19 at 18.38.56.png Screen Shot 2015-02-19 at 18.38.18.png Screen Shot 2015-02-19 at 18.38.28.png Screen Shot 2015-02-19 at 18.38.40.png
     
  2. Quiet-Pixel

    Quiet-Pixel

    Joined:
    Aug 23, 2013
    Posts:
    48
    To answer part of your question, about calculating the orthographic camera width, I do this in my code by getting the aspect ratio of the Screen.height to Screen.width, as follows (in C#):

    float screenAspect = (float) Screen.width / (float) Screen.height;
    float camHalfHeight = orthoCam.orthographicSize;
    float camHalfWidth = screenAspect * camHalfHeight;
    float camWidth = 2.0f * camHalfWidth;

    This gives the width of the camera in the world/scene coordinate system.
     
    bigSadFace likes this.
  3. bigSadFace

    bigSadFace

    Joined:
    Aug 18, 2014
    Posts:
    116
    Thanks. I'll give this a go.
     
  4. magicmonk

    magicmonk

    Joined:
    Sep 26, 2015
    Posts:
    1
    It works. Thanks.