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

Question Matching Orthographic Camera Size to Perspective Camera

Discussion in 'Scripting' started by Scullysaurus, Dec 8, 2022.

  1. Scullysaurus

    Scullysaurus

    Joined:
    Oct 23, 2018
    Posts:
    4
    Hi all, I've been trying to sort this one out for a bit, and I can't seem to find an answer via search.

    I'm making a 3d Game with Unity URP. I have a base perspective camera, and an overlay orthographic camera for the UI (the camera needs to be orthographic as the UI will be a mix of 2d and orthographic 3d elements) stacked on top.

    I'd like to change the orthographic camera's size to match the perspective camera, so that the elements stay "in place" regardless of resizing the window, resolution, or changing the perspective camera's FOV.

    Here's what I've tried to do since the orthographic size is in "Unity Units":

    Code (CSharp):
    1. public class OrthoCamAdjust : MonoBehaviour
    2. {
    3.     private Camera cam;
    4.     public Camera PerspectiveCam;
    5.  
    6.     void Start()
    7.     {
    8.         cam = gameObject.GetComponent<Camera>();
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         Vector3 middle = PerspectiveCam.ScreenToWorldPoint(new Vector3(0, 0, 0,3f));
    14.         Vector3 top = PerspectiveCam.ScreenToWorldPoint(new Vector3(0, PerspectiveCam.pixelHeight, 0.3f));
    15.  
    16.         middle = PerspectiveCam.transform.InverseTransformPoint(middle);
    17.         top = PerspectiveCam.transform.InverseTransformPoint(top);
    18.  
    19.         Vector3 result = top - middle;
    20.      
    21.         cam.orthographicSize = Mathf.Abs(result.y);
    22.     }
    23. }
    I've also tried using the perspective camera's FOV to figure out a solution, but I'm just not that great at math. Any pointers are much appreciated.
     
    Last edited: Dec 8, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    By mathematical necessity this will only match at ONE planar distance from the camera.

    If that doesn't immediately make sense, get out a piece of paper and draw your perspective and ortho frustums and see for yourself.

    Choose that distance and the answers you seek are found in trig functions such as Mathf.Tan or Mathf.Atan

    Remember trig functions accept values in radians, NOT degrees. Use Mathf.Rad2Deg/Deg2Rad to convert.
     
  3. Scullysaurus

    Scullysaurus

    Joined:
    Oct 23, 2018
    Posts:
    4
    Great call out, I should have mentioned it in the OP, already have a set distance picked, same as my 2d UI elements (let's say it's 4).

    I'll check out Mathf.Tan and Mathf.Atan, though my trigonometry is as rusty as the titanic, so if you have a formula in mind you'd be saving me a considerable number of math-infused tears.
     
  4. Scullysaurus

    Scullysaurus

    Joined:
    Oct 23, 2018
    Posts:
    4
    I'm now realizing I might be explaining my problem incorrectly. Maybe what I'm looking for is the other way around. Here are some pictures to illustrate:



    Looking good here.



    I've changed the horizontal scale of the viewport, and now it's bad.
     
    Last edited: Dec 8, 2022
  5. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    Is the obj in red circle drawn using orthographic camera same as other white UI lines? In that case it seems like instead of matching orthographic camera size to perspective camera what you actually need is positioning object within orthographic camera relative screen edges. The various XToY functions in camera API can help implementing that https://docs.unity3d.com/ScriptReference/Camera.html .
     
    Scullysaurus likes this.
  6. Scullysaurus

    Scullysaurus

    Joined:
    Oct 23, 2018
    Posts:
    4
    Ah thank you, that is probably more what I'm looking for. The white lines are on a canvas, and they don't scale or move. What is XToY?
     
  7. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    XToY here would mean the "'ScreenToWorldPoint' 'ViewPortToScreenPoint', etc"-functions.
    Also, remember that ScreenSpace and ViewPortSpace start at the bottom-left point of the screen, so (0, 0) would be the bottom-left corner, and not the middle of the screen.
    Thus, what you're currently calling 'middle' in your code is actually the bottom-left corner.

    From Camera.ViewportToScreenPoint:
     
    Scullysaurus likes this.