Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

How to programmatically change virtual cam's orthographic size?

Discussion in 'Cinemachine' started by hippo_san, Oct 10, 2017.

  1. hippo_san

    hippo_san

    Joined:
    Oct 29, 2014
    Posts:
    18
    I want to change virtual cam's orthographic size by script, but I cannot find how to access this variable. Then I tried to modify main camera's, like Camera.Main.orthographicSize = 1.0f; But it seems not work. Please help, thanks!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,134
    vcam.m_Lens.OrthographicSize = bla;
     
    shreyanshanchlia likes this.
  3. hippo_san

    hippo_san

    Joined:
    Oct 29, 2014
    Posts:
    18
    Thanks for the nice and quick reply!
     
  4. Finn_Alberts

    Finn_Alberts

    Joined:
    Jan 17, 2019
    Posts:
    4
    @Gregoryl Hi there, I'm trying to do the same thing, but when I type vcam.m_Lens.OrthographicSize = 8; It says "The name 'vcam' does not exist in the current context". I'm quiet new to Unity, so it might just be me being retarted. Can you help me out?
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,134
    "vcam" in this context is just shorthand to refer to current virtual camera in the current script context. Not meant to be taken literally. Can you post the script snippet where you're trying to set the value?
     
  6. Finn_Alberts

    Finn_Alberts

    Joined:
    Jan 17, 2019
    Posts:
    4
    @Gregoryl
    Code (CSharp):
    1. public void SetFov(int fovIndex)
    2.     {
    3.         vcam.m_Lens.OrthographicSize = fovIndex;
    4.  
    5.     }
    This is in my settings menu script
     
    Last edited: Jan 31, 2019
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,134
    You have to get the current Camera, then get its CinemachineBrain component, then query the brain for the current active Virtual Camera, then set its ortho size. There is no hard and sure way to do this, as it depends on how you've set up your game.

    One possible way:
    Code (CSharp):
    1. var camera = Camera.main;
    2. var brain = (camera == null) ? null : camera.GetComponent<CinemachineBrain>();
    3. var vcam = (brain == null) ? null : brain.AciveVirtualCamera as CinemachineVirtualCamera;
    4. if (vcam != null)
    5.     vcam.blablabla...
    6.  
     
  8. Finn_Alberts

    Finn_Alberts

    Joined:
    Jan 17, 2019
    Posts:
    4
    I've figured it out. Thank you for your help!
     
  9. unhuge

    unhuge

    Joined:
    Jan 3, 2018
    Posts:
    2
    i have no idea how to change the value of orthographic size (script) when the camera/player is move but when camera/player stop move, the orthographic size return to their main value. can anyone help me? i need help...
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,134
    Can you describe your setup a little, and explain more clearly what you are trying to do?
     
  11. realbambiiyt

    realbambiiyt

    Joined:
    May 29, 2018
    Posts:
    1
    Hey Finn, could you describe how you went about fixing this? I've also run into this problem haha.
     
  12. joshuapikner

    joshuapikner

    Joined:
    Monday
    Posts:
    1
    Lets say you were starting from a blank slate, how would you go about setting up the virtual camera in the most optimal way to make this work? Also, when would the if (vcam != null) statement activate?
     
    Last edited: Dec 2, 2019 at 11:30 PM
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,134
    If the slate is blank, you can start by watching these videos: