Search Unity

Switching between Unity Camera and Cinemachine(Virtual) Camera

Discussion in 'Cinemachine' started by chrisk, Feb 10, 2020.

  1. chrisk

    chrisk

    Joined:
    Jan 23, 2009
    Posts:
    639
    Hi, I have a situation that I need to switch between Unity Camera and Virtual Camera.
    A problem is what for example, when switching from Unity Camera to Virtual Camera, the Virtual Camera's starting position is different from the Unity Camera so that the camera position changes suddenly.

    My question is, how do I set the Virtual Camera's initial position to Unity Camera position when the switching happens, and vice versa.

    I tried the following but it didn't work.
    Code (CSharp):
    1.  
    2. if(cinemachineBrain.ActiveVirtualCamera != null)
    3. {
    4.     cinemachineBrain.ActiveVirtualCamera.VirtualCameraGameObject.transform.position = unityCamera.transform.position;
    5. }
    6.  
    I'll appreciate for your help.

    Thanks.
     
  2. streeetwalker

    streeetwalker

    Joined:
    Jun 4, 2013
    Posts:
    55
    is your virtual camera set to a dolly track, to follow an object, or otherwise engaged? I believe those will interfere with your positioning...
     
  3. chrisk

    chrisk

    Joined:
    Jan 23, 2009
    Posts:
    639
    The Cinemachine setup is ok, I think.

    I use cinemachineBrain.enabled = true to switch from Unity Camera to Virtual Camera.
    It switches the camera ok but the position does not start at the same place. I need to set the Virtual Camera to start where Unity Camera is, somehow.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    You can't move the virtual cameras manually if you've given them procedural behaviours because they will override whatever you do - that's their job.

    You can create a passive virtual camera with "Do Nothing" in Aim and Body. Because it has no procedural behaviours, it will stay where you put it. Instantiate it at the same position/rotation as the camera when you enable the Brain, so that it starts out as the active virtual camera. You can even make it a child of the Camera. Then immediately lower its priority (or activate some other virtual camera) to initiate a blend away from it.

    In fact, the normal way to have a custom script-controlled camera is to have the script drive that passive vcam instead of the camera. Then you can just leave the Brain on all the time, but have the passive vcam be enabled when you want your script to directly control the camera.
     
    Last edited: Feb 10, 2020
    Lars-Steenhoff likes this.
  5. chrisk

    chrisk

    Joined:
    Jan 23, 2009
    Posts:
    639
    Hmm.. thanks for the answer but it seems overly complicated for a simple use case. I not sure why you can't manually set the position(just once) of the procedural camera. I think we should be able to set it to an arbitrary location(position/rotation) and it will blend it back to a position where the procedural camera wants to be at by user preset, kinda like a rubber band effect. I guess what we need is to blend between a location(position/rotation) and a virtual camera, not just between virtual cameras.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,375
    Not complicated
     
unityunity