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

Question Main Camera Orthographic doesn't work after build

Discussion in 'Scripting' started by rolang, Aug 25, 2022.

  1. rolang

    rolang

    Joined:
    Jan 14, 2022
    Posts:
    6
    Hello, I'm newbie with Unity3D.

    I want to change my main camera's projection "Perspective" to "Orthographic".

    then i use this code for this work, very simple


    Code (CSharp):
    1.  public void persp_ortho_Btn_Clicked()
    2. {
    3.      if (!Camera_Controller.shared_instance.isOrthoCamera)
    4.      {
    5.          Camera_Controller.shared_instance.isOrthoCamera = true;
    6.          Camera.main.orthographic = true;
    7.          ui_camera.orthographic = true;
    8.      }
    9.      else
    10.      {
    11.          Camera_Controller.shared_instance.isOrthoCamera = false;
    12.          Camera.main.orthographic = false;
    13.          ui_camera.orthographic = false;
    14.      }
    15. }

    This works pretty well when i run this in Editor's Game Window.





    But when i build this project and run .exe
    then there is no view matrix change





    So i debugged this.
    And i found that it seems Camera.main.orthographic property is changing well in built exe. False to True, True to False, but view doesn't show that matrix (maybe).

    Why this happen and how can i fix this?

    I'm using Unity 2020.3.33f1 Personal and Project's Render Pipeline is URP.

    Thanks for your comments!!!
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Easiest way would just have two cameras and swap between them instead.
     
  3. rolang

    rolang

    Joined:
    Jan 14, 2022
    Posts:
    6
    Thanks for the reply !
    Yes, but i just thought using 2 camera rendering SAME objects may cause lower framerate.. eventhough turn off Camera components.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Pretty sure if a component is inactive, it's inactive.

    Only one way to find out, isn't there? Shouldn't take you long to test.
     
  5. rolang

    rolang

    Joined:
    Jan 14, 2022
    Posts:
    6
    oh well, now i got this.
    First, as you said i tested it, then the framerate average became a little bit lower.

    And my problem, the main camera doesn't change to orthographic actually,
    the script of camera interface needs

    Code (CSharp):
    1. public Camera
    variable to be changed to orthographic.
    When i do this with

    Code (CSharp):
    1. Camera.main.orthographic = true;
    the Editor's Game simulate works well but not the built one.
    But with specific variable of camera that i want to change projection mode, like

    Code (CSharp):
    1. public Camera mainCamera;
    2. mainCamera.orthographic = true;
    This works well Editor and Built exe both.

    Maybe Camera.main.~~ things is supposed to search Main Camera in current view, not that camera i want to search as main, So.. i have to set SPECIFIC camera to the script.

    Thanks for your attention anyway :D :D