Search Unity

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:
    23
    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!
     
    BMRG14 likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    vcam.m_Lens.OrthographicSize = bla;
     
    BMRG14, zeimhall, medts61 and 7 others like this.
  3. hippo_san

    hippo_san

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

    Finn_Alberts

    Joined:
    Jan 17, 2019
    Posts:
    5
    @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:
    7,711
    "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:
    5
    @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:
    7,711
    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.ActiveVirtualCamera as CinemachineVirtualCamera;
    4. if (vcam != null)
    5.     vcam.blablabla...
    6.  
     
    Last edited: Mar 25, 2020
    simoneavogadro likes this.
  8. Finn_Alberts

    Finn_Alberts

    Joined:
    Jan 17, 2019
    Posts:
    5
    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:
    7,711
    Can you describe your setup a little, and explain more clearly what you are trying to do?
     
  11. h38

    h38

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

    joshuapikner

    Joined:
    Dec 2, 2019
    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
  13. Gregoryl

    Gregoryl

    Unity Technologies

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


     
  14. thereal_semo

    thereal_semo

    Joined:
    Feb 28, 2021
    Posts:
    1
    I don't know, but this does not work for me. If I am doing this, I keep getting "The type or namespace name 'CinemachineBrain' could not be found" as a reply.
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Do you have
    using Cinemachine;
    at the top of your script?
     
  16. Lil_Potatoe

    Lil_Potatoe

    Joined:
    Feb 2, 2021
    Posts:
    16
    Hey
    I'm trying to change my orthographic size in my virtual camera when my main character collides with an object. I do not really know where to start since I just started coding this year, so any ideas?
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    CinemachineVirtualCamera vcam;
    vcam.m_Lens.OrthographicSize = bla
     
  18. Lil_Potatoe

    Lil_Potatoe

    Joined:
    Feb 2, 2021
    Posts:
    16
    Hey when I tried that out it gave me these errors
    - "Invalid token '=' in class, record, struct, or interface member declaration [Assembly-CSharp, Assembly-CSharp]",
    - "Invalid token ';' in class, record, struct, or interface member declaration [Assembly-CSharp, Assembly-CSharp]",
    - "Invalid token '}' in class, record, struct, or interface member declaration [Assembly-CSharp]",

    I don't really know why they popped up so here's my code. Also thank you in advance for helping me.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Cinemachine;

    public class ZoomOut : MonoBehaviour
    {
    CinemachineVirtualCamera vcam;
    vcam.m_Lens.Orthographicsize "=" bla ";"

    "}"

    the things inside " " are what had the red underlines
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    I think you took my previous email a little too literally. I was assuming you knew a little more about scripting than you do.

    I would strongly advise you to learn a few basics before continuing. It will help a lot. You could start here: https://unity3d.com/learning-c-sharp-in-unity-for-beginners
     
  20. Lil_Potatoe

    Lil_Potatoe

    Joined:
    Feb 2, 2021
    Posts:
    16
    Ok so new question, How would I get components of my virtual camera. Like what are some ways I can reference my active virtual camera so I can modify its components.
     
  21. chris_4308

    chris_4308

    Joined:
    Jun 21, 2022
    Posts:
    2
    I've changed cinemachine orto-size trough Animation :)
     
    awsapps likes this.