Search Unity

Fixed camera width?

Discussion in 'Scripting' started by Blobfish, Mar 16, 2013.

  1. Blobfish

    Blobfish

    Joined:
    Feb 28, 2013
    Posts:
    30
    Hello,

    I'm making a game for android/ios and i want the width to be the same on all phones. I'm using an orthographic camera and here is an image illustrating what i want to do (the black bars are walls and the red squares are the visible area):
     

    Attached Files:

  2. Blobfish

    Blobfish

    Joined:
    Feb 28, 2013
    Posts:
    30
    Anyone? I have googled for hours now and i can't come up with a solution. There must be way to do this, right? All i want to do is to make it show less/more on the height instead of the width.

    EDIT: Solved it myself
     
    Last edited: Mar 17, 2013
  3. stridervan

    stridervan

    Joined:
    Nov 11, 2011
    Posts:
    141
    Code (csharp):
    1.  
    2. Camera.main.aspect = width / Screen.height;
    3.  
    Aspect is basically the aspect ratio of the view area which is the width divided by the height.
     
    Last edited: Mar 17, 2013
  4. amzg-maki

    amzg-maki

    Joined:
    Mar 12, 2013
    Posts:
    12
    Can you please show how you solved this? As I now face the same problem with the camera width while working on my game for Android/iOS. :(
     
  5. Blobfish

    Blobfish

    Joined:
    Feb 28, 2013
    Posts:
    30
    Set the game to a resolution you want the game to look ideal in (for example 480x800) and then zoom the camera so it shows what you want it to show. Now, take ortographicSize/(SceenHeight/ScreenWidth). This will be your constant.

    Then, simply use this code to set the size:
    Code (csharp):
    1.  
    2. double size = yourConstant*(camera.GetScreenHeight()/camera.GetScreenWidth());
    3.  
     
    Last edited: Mar 21, 2013
  6. amzg-maki

    amzg-maki

    Joined:
    Mar 12, 2013
    Posts:
    12
    Oh thanks so much! I think I got it now. As in my case I want to set the game to 720x480, so I did it like this:
    Code (csharp):
    1. Screen.SetResolution(720,480,false);
    2. float screenRatio = camera.orthographicSize/(480f/720f);
    3. camera.orthographicSize = screenRatio*(camera.GetScreenHeight()/camera.GetScreenWidth());
    Am I missing something here? (Didn't test this yet on different devices tho)
     
  7. Blobfish

    Blobfish

    Joined:
    Feb 28, 2013
    Posts:
    30
    screenRatio should be a constant so you need to replace camera.orthographicSize with the size of the camera when it fits the way you want it.
     
  8. amzg-maki

    amzg-maki

    Joined:
    Mar 12, 2013
    Posts:
    12
    Haha just take a look at my code today and realize I've made this line useless:
    Code (csharp):
    1. camera.orthographicSize = screenRatio*(camera.GetScreenHeight()/camera.GetScreenWidth());
    Since nothing can change if I calculate "screenRatio" using "camera.orthographicSize". My bad :rolleyes: Will need to take some Maths classes. Thanks again for figuring it out. :D
    Cheers!