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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Match Perspective and Ortho Cameras

Discussion in 'Scripting' started by morton fink, Aug 8, 2013.

  1. morton fink

    morton fink

    Joined:
    Mar 13, 2013
    Posts:
    86
    i have a ortho camera looking at a plane positioned and set so that the gameview and the plane are matched (upper-lefft corner of the plane is in the upper-left corner of the screen and the lower-right corner of the plane is at the lower-right corner of the screen)

    now i want a perspective camera to show the plane in the same way. i know this can be done by hand but i was wondering:
    is there a formula to set a perspective camera-view to match a ortho camera-view at a given distance?
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You can probably solve it using right triangle math.
    The base is the distance from plane, height is the orthographic size of ortho camera.
    Then you Can solve the base/hypo angel, which is half your perspective camera fov.
    Make syre to match camera rotations. And position camera at plane center, and move it back with your distance.
     
    samlletas likes this.
  3. morton fink

    morton fink

    Joined:
    Mar 13, 2013
    Posts:
    86
    that's simple. thanks.
     
  4. samlletas

    samlletas

    Joined:
    Jan 2, 2016
    Posts:
    30
    I know this is a very old thread, but in case anyone needs this you can use the following script which is based on @ThermalFusion approach:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraFOV : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     private float orthographicSize = 5;
    7.  
    8.     private void Start()
    9.     {
    10.         Camera camera = GetComponent<Camera>();
    11.         float distance = Mathf.Abs(camera.transform.position.z);
    12.         camera.fieldOfView = Mathf.Atan2(orthographicSize, distance) * Mathf.Rad2Deg * 2f;
    13.     }
    14. }
    15.  
    Note: The code assumes the plane's Z position is 0.