Search Unity

Bug CinemachineVirtualCamera Damping and Smoothing not working

Discussion in 'Cinemachine' started by SimonsCat, Nov 19, 2021.

  1. SimonsCat

    SimonsCat

    Joined:
    Mar 11, 2015
    Posts:
    49
    Hi,

    We are testing the Cinemachine Example Scenes from the Cinemachine Package.

    We opened the 3rdPersonWithAimMode sample scene and set 2 parameters of the Cinemachine Collider extension:
    - Strategy to Pull Forward Camera
    - Damping to 10

    As you can see in the video, the camera does some sort of damping but the camera moves instantly back and smoothing sideways at the same time. The expected behavior is to preserve the direction and smooth the trasition backward to the original distance set.



    The second bug captured is the Smoothing time... we set the smoothing time to 2 seconds but the camera ignores this setting and starts to bull back the camera immediately... this feature worked as expected in CM 2.7.9



    Is this behavior expected?
     
    Last edited: Nov 19, 2021
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    No, these behaviours are not expected. We will look into them. Thanks for bringing it up.
     
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    We could repro the smoothing problem but not the damping thing. What version of CM are you using?
     
  4. SimonsCat

    SimonsCat

    Joined:
    Mar 11, 2015
    Posts:
    49
    We used Unity 2020.3.18f1 and CM 2.8.2... we noticed this in our production scene first, when we tried to setup the VC, then we reproduced the same in a completely empty project, loading just the CM sample scenes.

    btw.. we also noticed that the smoothing worked only once, the first time the camera needed to do the smoothing and then not anymore. The dumping thing is quite consistent and we did not notice that it would work under certain conditions. When testing try to rotate the camera quickly and set the damping time to max so you can observe the movement slowly.
     
    Last edited: Nov 29, 2021
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Thanks for the description. We have fixed the smoothing bug,

    The damping is behaving as expected for this setup. Why don't you use the 3rdPersonFollow rig instead? It has its own built-in collider that is designed specifically for 3rdPerson cameras. Have a look at the AimingRig sample.
     
  6. SimonsCat

    SimonsCat

    Joined:
    Mar 11, 2015
    Posts:
    49
    Hi Gregoryl,

    I waited for the new Cinemachine release and retested the cameras. (CM 2.8.4, Unity 2020.3.25f1, Standard Pipeline)
    I created a new empty project and imported the CM Samples. No other code added except default Unity packages.

    To test the Cinemachine Collider Extension I opened the "3rdPersonWithAimMode" Scene.
    1. Disabled the Noise component on the VC "CM 3rdPerson Normal" (set to none)
    2. Changed the CM Collider Strategy property to "Pull camera forward"
    3. default parameters for smoothing time and damping left on 0

    Everything looks ok and seems to work normal.

    4. Changed Smoothing time to 2 and Damping to 10 (exaggerated values to see better the behavior)

    PROBLEM 1:
    When I exit the collision with the nearby wall the smoothing now works every time (not just the first time)... I always see the 2 sec delay before the camera pulls back to a proper distance... however, the pulling back feature is the thing that is broken.
    The Pull camera forward strategy means that the camera gets pulled forward, towards the point the camera is looking at, in front of the collider that obscures the camera view. Right? This part seems ok.
    When I rotate the camera out of the collision, the VC waits 2 sec (as it is set) and then starts to pull back to the original distance... also seems fine, BUT if you rotate the camera (Aim POV) DURING the pull back phase, let's say for another 90 deg, the bull back direction stays locked in WORLD SPACE and the camera starts floating sideways. Looking in the right direction, but moving to the side instead of moving backward, away from the look point.

    PROBLEM 2:
    Another thing to mention: If I set the smoothing time and the damping time, there is a glitch/camera shaking every time I rotate the camera out of collision. The shaking happens only if I rotate the camera the whole time after the camera leaves the collision and before the smoothing time expires and the camera starts to pull back. Then the shaking stops. If the smoothing and damping values are = 0 then there is no shaking.


    We want to use this kind of camera for the free look walking camera, because it has all the features we need and the expected behavior is exactly what we are looking for. And for the actual Aim/Shooting camera we are using the camera you suggested - 3rdPersonFollow rig.
     
    Last edited: Dec 27, 2021
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Why not use 3rdPersonFollow rig for both cameras? What does this camera do that the 3rdPersonFollow does not?
     
  8. SimonsCat

    SimonsCat

    Joined:
    Mar 11, 2015
    Posts:
    49
    Hi Gregoryl,

    We finally managed to do some research on this damping matter. We had an issue with damping and a connected collision problem, that was also caused by this damping issue on the CinemachineCollider extension.

    Tested and fixed on Unity 2020.3.33 and Cinemachine 2.8.6.
    camera_Screenshot 2022-08-05 113509.jpg

    collider_Screenshot 2022-08-05 113452.jpg

    The strange dumping behavior was observed with the Pull Forward Camera strategy. We expected the camera to do the dumping only in the Forward/Backward direction, but instead the damping was done in all directions + we discovered that when the camera is rotating during the damping phase the calculations for the final position of the camera were wrong. This is causing unwanted camera movements, wrong damping direction and even penetration of the colliders by the camera during some frames (when there is a damping in progress).

    After investigating the CinemachineCollider.cs code we did the following changes:

    we replaced the line 284:
    Code (CSharp):
    1. extra.previousDisplacement = Quaternion.Euler(state.PositionDampingBypass) * extra.previousDisplacement;
    with:
    Code (CSharp):
    1. extra.previousDisplacement = state.RawOrientation.Diff( extra.previousCameraOrientation ) * extra.previousDisplacement;
    where the Diff quaternion extension is:
    Code (CSharp):
    1. public static Quaternion Diff( this Quaternion to, Quaternion from )
    2.    {
    3.       return to * Quaternion.Inverse( from );
    4.    }
    AND we added and extra line (after line 339 and a variable in the class VcamExtraState):
    Code (CSharp):
    1.  extra.previousCameraOrientation = state.RawOrientation;
    that stores the current rawOrientation that will be used in the next frame (in the extension above)

    The previousDisplacement is now properly calculated based on the camera rotation and now the damping seems to work fine (at least for the Pull Forward Camera strategy that we use, the other strategies might need other solutions. We did not tested as we do not use them). No more unwanted damping directions and no more collision penetrations. Finally the smoothing and damping does what we expected.

    In case other devs experience the same issue and this cannot be addressed in the official Cinemachine Package, you can at least make this custom adjustments.

    I hope we got this right?

    ;)
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Thank you for this, it's very interesting. We will dig into it and have a think.
     
    SimonsCat likes this.
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @SimonsCat Do you have a simple example scene that shows the problem that this fix addresses?
     
  11. SimonsCat

    SimonsCat

    Joined:
    Mar 11, 2015
    Posts:
    49
    Yes, it's basically the Cinemachine example project... I will prepare it.
     
    Gregoryl likes this.
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Yes, that will be helpful. Unfortunately we can't incorporate your CinemachineCollider modification as-is, because it's very specific to your use-case and it breaks the collider in other situations. We are experimenting with an alternate strategy that is more friendly to other vcam styles, and we want to be sure that it addresses your use-case also.

    EDIT: looking back in the thread, I see that you described the sample scene you are using. I will try to repro your issue with that.
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    ok, so I reproduced the issue with the sample scene described at the top of this thread. A better fix for the issue is to leave collider alone, but change CinemachinePOV so that it reports its activity to state.PositionDampingBypass. That way, CM collider will correctly account for it using its original code.

    In CinemachinePOV, line 145, add this code:
    Code (CSharp):
    1.             if (VirtualCamera.PreviousStateIsValid)
    2.                 curState.PositionDampingBypass = UnityVectorExtensions.SafeFromToRotation(
    3.                     m_PreviousCameraRotation * Vector3.forward,
    4.                     rot * Vector3.forward, curState.ReferenceUp).eulerAngles;
    5.             m_PreviousCameraRotation = rot;
    and also, at line 68, add this member variable:
    Code (CSharp):
    1.         Quaternion m_PreviousCameraRotation;
     
    Last edited: Aug 17, 2022
  14. SimonsCat

    SimonsCat

    Joined:
    Mar 11, 2015
    Posts:
    49
    Interesting, we will try your solution and let you know ;) Thanks!
     
    Gregoryl likes this.
  15. SimonsCat

    SimonsCat

    Joined:
    Mar 11, 2015
    Posts:
    49
    Yes, that solves the incorrect behavior properly ;) with the same result as we did with our ugly hack :)

    Will you include this fix in the package?

    Btw... now that the pulling forward and backward works you can observe a small glitch of the camera if you move this POV camera SLOWLY towards the ground. When the collider resolves the penetration into the ground for the first time (frame) and moves the camera out of collision there is a small glitch. A camera jumps a bit between the frame when was still out of collision with the ground and the next frame where the camera is offset a bit because it's colliding with the floor. This glitch can be observed on walls too but it's less visible.

    In my opinion the glitch happens because the CM collider moves the camera in the direction of the surface normal instead in the direction perpendicular to the vector( VC position , LookAt ).o_O
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    We have pushed a fix onto CM trunk, but unfortunately it's too late to get into the current release in progress. It will be in the one after that.

    For the glitch issue, would you be willing to file an official bug report so that it gets tracked properly?
     
  17. SimonsCat

    SimonsCat

    Joined:
    Mar 11, 2015
    Posts:
    49
    Great news ;)

    We will prepare a bug report for the glitch too.

    Thank you for the assistance.
     
    Gregoryl likes this.
  18. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    @SimonsCat Thank you for your efforts with these issues. We really appreciate it.