Search Unity

Inherit rotation of VCam before blending?

Discussion in 'Cinemachine' started by Mathius123, Feb 27, 2020.

  1. Mathius123

    Mathius123

    Joined:
    May 21, 2018
    Posts:
    14
    I am trying to figure out how to force a Cinemachine Virtual Camera to adopt the rotation (not position) of another Cinemachine Virtual Camera that it transitions from. I am using Unity 2019.1.9f1 and Cinemachine 2.3.4. Here is my setup:

    I have a Cinemachine Brain game object called MainCameraBrain that shares the same parent as a Cinemachine Virtual Camera called Main VCam. This Main VCam functions as my primary virtual camera in game and has the lowest priority.

    Cinemachine Brain
    CinemachineBrain.JPG

    Main VCam
    VCam1.JPG

    I have a Cinemachine Virtual Camera named Static VCam with a fixed Look At target used as a middleman for blending to all of my other Cinemachine Virtual Cameras where the view is centered in the middle of the screen. This makes it so no matter where the Player is looking, before I start any blends it always goes to this Static VCam first.

    Static VCam
    VCam2.JPG

    As an example, I have a Cinemachine Virtual Camera named Inject VCam with a fixed Look At target that forces the camera to look down at about 80 degrees so the Player is forced to watch the injection animation that takes place where medicine is injected via syringe into the Player's arm.

    Inject VCam
    VCam3.JPG

    Let's say the Player is looking directly up at the sky and wants to perform the injection. I activate the Static VCam game object and the Main VCam then blends to the Static VCam which looks dead center in the screen. I start the injection animation and enable the Inject VCam and the blend has the Player looking down towards the ground. When the injection animation is over, I disable the Inject VCam and the blend transitions back to the Static VCam with the Player looking straight ahead. I then disable the Static VCam and the Player goes back to looking up at the sky where the Main VCam first started before all these transitions.

    What I want to happen is before the Static VCam transitions BACK to the Main VCam as the last transition, I want the Main VCam to inherit that centered view / rotation of the Static VCam so the Player can continue to aim from that starting point and not be forced to go look back up at the sky before returning aim control back to the Player.

    Here is what I have tried so far:
    • Checking the Inherit Position checkbox on the Main VCam, and even went into the actual CinemachineVirtualCamera script and uncommented line 528 that deals with setting the rotation of the fromCam. No luck.
    • Forcing the Main VCam to use the same Look At target as the Static VCam via code before it transitions, then remove the Look At target via code. No luck. As soon as I remove the Look At, the Main VCam just blends back to looking up at the sky no matter when I do this.
    Any help would be greatly appreciated.
     
    Last edited: Feb 27, 2020
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Created a project, and before blending from vcamS to vcamM, I set vcamM's transform to transform values of vcamS. I get what I think you want.
    vcamS represents your static vcam.
    vcamM represents your main vcam.

    Just to clarify, you main vcam does nothing, right? You directly control it's transform with another script (Aim Sway maybe)?
    Can't you set your Main Vcam position and/or rotation to your static camera's position and rotation, before the blend from static to main happens?
     
    Last edited: Feb 28, 2020
  3. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    if it does not work, could you send me a simple project where I can reproduce the issue. I'll have a look under the hood ;)
     
  4. Mathius123

    Mathius123

    Joined:
    May 21, 2018
    Posts:
    14
    Sorry, I should have mentioned that I tried that. Setting the transform directly of the Main VCam does nothing. It was my understanding from Gregory that this is not possible with Cinemachine Virtual Camera as its algorithm overrides any direct calls to its transform. I was doing this previously BEFORE disabling Static VCam and it always went back to where the Main VCam was looking:

    mainVCam.transform.rotation = staticVCam.transform.rotation;
    staticVCam.setActive(false);

    I was hoping the method OnTransitionFromCamera inside the CinemachineVirtualCamera script on line 519 would be the solution, as I saw that line 528 had the code:

    //transform.rotation = fromCam.State.RawOrientation;

    Uncommenting this does nothing. No matter what I try to do, the second I disable the Static VCam, it automatically blends back to Main VCam wherever it was looking before I started the transitions.
     
    Last edited: Feb 28, 2020
  5. Mathius123

    Mathius123

    Joined:
    May 21, 2018
    Posts:
    14
    @Gregoryl - any thoughts? I tried using the following before disabling the staticVCam which would force the transition back to where mainVCam was aiming and it didn't work:

    mainVCam.gameObject.GetComponent<CinemachineVirtualCamera>().PreviousStateIsValid = false;

    No matter what I try the camera goes back to aiming in the direction whatever mainVCam was aiming and I don't want that.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,712
    I think your problem is the rig hierarchy. In my view it's a mistake to have your main camera live inside your rig along with the main vcam.

    If I understand correctly, we're dealing with a first-person game, so all your vcams should be positioned at the same spot, but looking at different things. Is that right?

    So, the rig should be controlling only an empty placeholder GameObject, which will retain its position and orientation as set by the rig.

    Next, put the main camera with the brain in a completely separate game object. The brain will take care of positioning itself according to the current active vcams.

    The main vcam should have the empty Rig gameobject as Follow Target, and have HardLockToTarget and SameAsFollowTarget in Body and Aim, respectively. Then, it will exactly match the rig, always. You might be able to get away with putting this vcam directly inside the rig, as a component on the empty rig gameobject, with DoNothing in Aim/Body, as you have it now.

    For the Look Elsewhere vcams, they should be separate GameObjects with the rig as Follow target, and HardLockToTarget in Body. Then, put whatever in LookAt target and Aim. You probably don't even need the intermediate LookAt vcam.

    Then all the blending should work as you want it to, and the main vcam will remember its position.