Search Unity

Z of camera depend on screen height

Discussion in 'Scripting' started by herbie, Aug 25, 2014.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    I have a gameobject.
    What I want is that the size of this gameobject is always the same with respect to the screen height of the device where it's displayed. So with a device with a large screenheight, the gameobject is more zoomed in than a device with a small screenheight.

    What I have now is this but it's not working good:

    Code (JavaScript):
    1. public var Zcamera: Camera;
    2. public var rad: float;
    3. public var deg: float = 30.0;
    4. function Start()
    5. {
    6.    rad = deg * Mathf.PI / 180;
    7.    Zcamera.transform.position.z = Screen.height / (Mathf.Tan(rad));
    8. }
    Does anybody know how I can do this?
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Just to make sure I understand, if someone has a 720p display vs a 1080p display, you want the picture displayed to be the same right? Cameras should automatically fit the frame based on screen height.

    If the aspect ratios are different, then you might have to mess around with the camera.