Matching Orthographic Camera Size to Perspective Camera in Unity | C#

Matching Orthographic Camera Size to Perspective Camera in Unity | C#


In Unity, you can match the orthographic camera size to the perspective camera by adjusting the "Size" property of the orthographic camera. This property determines the vertical viewing area of the camera, and setting it to the same value as the perspective camera's field of view will match their respective viewports.

 

Methode 1:

In Unity, you can match the size of an orthographic camera to that of a perspective camera by using the following steps:

  1. Create a new script and attach it to the orthographic camera.

  2. In the script, create a reference to the perspective camera by using the GameObject.Find() method.

  3. Calculate the field of view (FOV) of the perspective camera by using the Camera.fieldOfView property.

  4. Calculate the size of the orthographic camera by dividing the FOV by 2 and then dividing that number by the camera's aspect ratio.

  5. Set the size of the orthographic camera by using the Camera.orthographicSize property.

Here is an example of what the script might look like:

using UnityEngine;

public class OrthoCameraSize : MonoBehaviour
{
    public Camera perspectiveCamera;

    void Start()
    {
        // Find the perspective camera in the scene.
        perspectiveCamera = GameObject.Find("PerspectiveCamera").GetComponent<Camera>();

        // Calculate the FOV of the perspective camera.
        float fov = perspectiveCamera.fieldOfView;

        // Calculate the size of the orthographic camera.
        float orthoSize = fov / 2 / perspectiveCamera.aspect;

        // Set the size of the orthographic camera.
        GetComponent<Camera>().orthographicSize = orthoSize;
    }
}


Methode 2:

To do this, first select the perspective camera in the hierarchy or the scene view. Then, in the inspector, look for the "Field of View" property. This value represents the camera's field of view in degrees, and it determines how much of the scene the camera can see.

Next, select the orthographic camera in the hierarchy or the scene view. In the inspector, look for the "Size" property, and set it to the same value as the perspective camera's field of view. This will match the vertical viewing area of the two cameras, and they will have the same viewport size.

Keep in mind that the orthographic camera will still have a different perspective than the perspective camera, as the former uses a flat, two-dimensional view, while the latter uses a more realistic, three-dimensional perspective. However, matching the camera sizes will ensure that they have the same viewport and will make it easier to compare their respective views of the scene.


 Methode 3:

To match the orthographic camera size to the perspective camera in Unity, follow these steps:

  1. Select the orthographic camera in the hierarchy and adjust its size in the Inspector panel under the Transform section.

  2. Next, select the perspective camera in the hierarchy and adjust its field of view in the Inspector panel under the Camera section.

  3. Use the scale tool in the Scene view to match the size of the objects in the perspective camera to the size of the objects in the orthographic camera.

  4. Adjust the position and rotation of the orthographic camera to match the perspective camera's view.

  5. Use the Preview window in the Inspector panel to compare the two cameras and fine-tune their settings until they match.