Search Unity

Programmatically turning on and off a VCam with a button in Cinemachine

Discussion in 'Cinemachine' started by unity_18jschmidt, Oct 23, 2017.

  1. unity_18jschmidt

    unity_18jschmidt

    Joined:
    Oct 21, 2017
    Posts:
    1
    I would like to be able to use Cinemachine to switch between two VCams in a script, perhaps by turning one camera on when a button is pushed, and off when it's released. One camera is focused on the whole play field, while the other (when the button is held) is zoomed in and focused on following a single object when it moves. I am just unfamiliar with Cinemachine's C# terminology and am not sure how to control the two cameras within the script.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Cinemachine chooses the current vcam from the pool of active vcams in the scene by selecting the one with the highest priority (the Priority field in the CinemachineVirtualCamera class). If multiple vcams share that highest priority, then the most-recently-enabled one will prevail.

    So you can control which vcam is active in several ways:
    • Toggle the active status of the vcam's game object. Your button could activate a vcam with higher priority than the default, which would cause that vcam to become active. Releasing the button could deactivate the vcam, returning control to whatever vcam had it before. Like temporarily pushing a vcam on a stack, then popping it. See https://unity3d.com/learn/tutorials/topics/scripting/activating-gameobjects.
    • Alternatively, pressing the button could raise the priority of a vcam, giving it precedence over the lower-priority vcams (from script, increase the value of the vcam's Priority field). Releasing the button would lower the Priority back to below default level, restoring the original vcam.
    • If you don't want stack-like behaviour, but would prefer a "me me me" approach, you could set all your vcams to the same priority, and use the API call vcam.MoveToTopOfPrioritySubqueue(), which will make the vcam dominant, as though it had just been enabled.
     
  3. TimHellmann

    TimHellmann

    Joined:
    Mar 6, 2011
    Posts:
    66
    We did something very similar for our game and found a very simple but excellent solution. It basically is a small extension to what Gregory mentioned first.

    Our idea was to store multiple virtual cameras in a List and switch between them via List index when pressing the 'Switch Camera' button. We did that with a simple SetActive(true/false) function that activates/deactivates the GameObject.
    That works perfectly and still triggers the smooth transition between cameras.
     
    Last edited: Oct 31, 2017
  4. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
    Necroing this thread, as I want to do the same thing.

    @Gregoryl vcam.MoveToTopOfPrioritySubqueue() doesn't seem to work. I've set all of my cams to the same priority, the default 10, but calling this function doesn't do anything, the cam at start is still the dominant one. I know this an older thread so I'll ask: is this still a viable way to adjust the dominant camera?
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Should still work. What version of CM are you using?
     
  6. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
    Ahh... bitten by 'Save During Play'. If this is enabled MoveToTopOfPrioritySubqueue() doesn't work.

    This seems odd to me. Is that a known bug? I would assume that only Inspector edits would be saved during play. I can see how that might be tricky given Unity's save + play model. Hrm, think I'll have to go with the manage list of vcams as I don't want to break save during play -- that's how all of Unity should be :D!
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Oh really?! First I've heard of that one! I'll look into it tomorrow, but off the top of my head I can't see how that could happen. SaveDuringPlay is completely passive during Play, and only kicks in when you exit play mode.
     
    v2-Ton-Studios likes this.
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    I bet it's a script ordering thing. Your MoveToTop is called before the last vcam gets enabled, and the last one to be enabled will dominate. Can you try, as an experiment, making your call on the second frame of the game?