Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Blending back a Cinemachine at the middle of a blend

Discussion in 'Cinemachine' started by ido100ido, Aug 20, 2020.

  1. ido100ido

    ido100ido

    Joined:
    Jun 7, 2020
    Posts:
    8
    I am building a 2d platformer game.

    I use 4 virtual CM to follow the player: one to the left of him, one to right, and two more CM placed downward for when the player is crouching.
    I am using Cinemachine Brain to blend between them, and I am doing so by activating and deactivating CMs.
    Usually, when the crouch button is pressed, The upper camera blends towards the lower one, and when it is unpressed, the lower camera blends to the upper one.
    The problem happens when the player rapidly crouch (press the button to crouch only for a single moment). In this case, the upper camera does not able to finish it's blending downward, and is already activated again and needs to return to it's location.
    this results in quick "snap" and not smoothed movement of the camera.

    How to make it so it Blends back regularly, in this case?

    P.S - sorry for the bad English, it's not my main language. also, I am using the latest cinemachine version.
     
    Last edited: Aug 20, 2020
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    CMBrain should blend back nicely. I tried reproducing the issue in a simple scene, but I could not.

    Could you send a gif/video showing the quick snap to clarify what you mean?
     
  3. ido100ido

    ido100ido

    Joined:
    Jun 7, 2020
    Posts:
    8
    Hello gaborkb,

    I as well tried to reproduce the problem in a new simple scene, and at first the problem did not occur and everything worked well.
    so I copied the script and cameras from the sample scene to my game, only to find out that in the actual game it does not work for some reason.
    I continued experimenting with the sample scene, until I found out what is causing the problem. In my game the player has a rigidbody, However, the player in the sample scene does not. I added a dynamic rigidbody to the player in the sample scene, and the problem reproduced.

    Here is a gif:

    The problem also occur when blending from right to left with a mid blend back to right, and for the opposite.

    I use the space key to switch between lower and upper cameras: Input.GetKeyDown for activating the lower, and Input.GetKeyUp to activate the upper.

    Here is the code I put on the main camera:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Example : MonoBehaviour
    6. {
    7.     [SerializeField] GameObject upperRightCM;
    8.     [SerializeField] GameObject lowerRightCM;
    9.     [SerializeField] GameObject upperLeftCM;
    10.     [SerializeField] GameObject lowerLeftCM;
    11.     void Start()
    12.     {
    13.      
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (Input.GetKeyDown(KeyCode.Space))
    19.         {
    20.             if (upperRightCM.activeSelf)
    21.             {
    22.                 lowerRightCM.SetActive(true);
    23.                 upperRightCM.SetActive(false);
    24.             }
    25.             else if (upperLeftCM.activeSelf)
    26.             {
    27.                 lowerLeftCM.SetActive(true);
    28.                 upperLeftCM.SetActive(false);
    29.             }
    30.         }
    31.         else if(Input.GetKeyUp(KeyCode.Space))
    32.         {
    33.             if (lowerRightCM.activeSelf)
    34.             {
    35.                 upperRightCM.SetActive(true);
    36.                 lowerRightCM.SetActive(false);
    37.             }
    38.             else if (lowerLeftCM.activeSelf)
    39.             {
    40.                 upperLeftCM.SetActive(true);
    41.                 lowerLeftCM.SetActive(false);
    42.             }
    43.         }
    44.  
    45.         if(Input.GetKeyDown(KeyCode.LeftArrow) && !upperLeftCM.activeSelf)
    46.         {
    47.             upperLeftCM.SetActive(true);
    48.             upperRightCM.SetActive(false);
    49.         }
    50.         if (Input.GetKeyDown(KeyCode.RightArrow) && !upperRightCM.activeSelf)
    51.         {
    52.             upperRightCM.SetActive(true);
    53.             upperLeftCM.SetActive(false);
    54.         }
    55.     }
    56. }
    Also, while I set on the problem, I noticed that the interruption of the blend is actually a sudden cut to the centre point of the followed target. I have no clue what makes it happen.

    I will appreciate your help.
     
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Thanks for the research and gif. There is clearly something wrong happening in the gif. I tried with Rigidbody on the player, but still could not reproduce the issue. Could you send me a project where I can reproduce the bug? I'll happily find the issue. ;)

    It might be due to some update order problem. You could try changing the Update Method or Blend Update Method on CinemachineBrain. But it may be something else. :confused:
     
    Last edited: Aug 21, 2020
  5. ido100ido

    ido100ido

    Joined:
    Jun 7, 2020
    Posts:
    8
    I tried changing the Updates- unfortunately- nothing..
    Is sending these three files enough? I never did this before.
     

    Attached Files:

    Last edited: Aug 21, 2020
  6. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Couple of problems with the setup:
    • Your virtual cameras (CM Upper Right, CM Lower Right, CM Upper Left, CM Lower Left) are children of the Main Camera. This is wrong.
      The reason is that virtual cameras (vcams) control the Main Camera's transform. But, because the vcams are children, their position is controlled by the Main Camera's transform through the hierarchy. It's kind of like a dog chasing its tail. :)
      To fix this, unparent your vcams, i.e. move them out from under the Main Camera. If you'd like to group your vcams, create an empty gameObject (put it to position (0,0,0) with rotation (0,0,0) and scale (1,1,1)) and put the vcams under this gameObject. I would also remove the CameraMovement.cs script from MainCamera and put it to this new gameObject.

    • Your virtual cameras have CinemachineBrain component on them. This is wrong.
      The reason is that CinemachineBrain is the link between the Unity Camera and your vcams in the scene. So only Unity Cameras need to have a CinemachineBrain component. In your case, only the Main Camera needs to have one.
      To fix this, remove CinemachineBrain component from each virtual camera.
    Now, after fixing the above, you can fix the snap/jitter problem by changing the Update Method to Late Update on CinemachineBrain. ;)

    Thanks for reporting your issue, because this will help us fine-tune our UX! :)

    I attached a fixed version (Assets.zip). If you are going to use this, you may need to relink your virtual cameras on the CameraMovement script, which is attached to Cinemachine Virtual Cameras gameObject.
     

    Attached Files:

    Last edited: Aug 21, 2020
    Gregoryl likes this.
  7. ido100ido

    ido100ido

    Joined:
    Jun 7, 2020
    Posts:
    8
    Thank you very much!
    That really did work like a charm.:)