Search Unity

Cinemachine Collider Stuttering

Discussion in 'Cinemachine' started by VX_PM, Jul 3, 2019.

  1. VX_PM

    VX_PM

    Joined:
    Aug 16, 2017
    Posts:
    16
    What should I do to fix the stuttering that happens when the camera collides with the terrain?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    It's not clear why you have that stuttering. Can you please post:
    • an image of your vcam inspector
    • and image of your CM Brain inspector
    • what version of CM and Unity you are using
     
  3. VX_PM

    VX_PM

    Joined:
    Aug 16, 2017
    Posts:
    16
    I'm using Cinemachine 2.3.4 with Unity 2019.1.0f2
    Here are the images:
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Hmmm... everything looks fine and dandy.
    It's not immediately obvious why you're getting the stuttering.
    Would you be willing to export a unitypackage containing a simple and lightweight reproduction of this problem, and send it to me?
     
  5. VX_PM

    VX_PM

    Joined:
    Aug 16, 2017
    Posts:
    16
    Sure! Here:
     

    Attached Files:

  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Thanks for packaging that up!

    Well, I think your use case pushes the collider way beyond its intended purpose. There aren't any settings that will get rid of the artifacts, unfortunately.

    We'll have to come up with a new strategy. Can you describe qualitatively how you'd like the camera to behave? For example: "as if a cameraman is walking along behind the character. When the character is obstructed by a hill, I'd like the camera to do X".
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    If you just want a camera that follows the terrain at a fixed height from the surface, then the solution is quite simple: replace the CinemachineCollider with an extension that hugs the camera to the terrain. Here is an example of one:
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. [AddComponentMenu("")] // Hide in menu
    5. [SaveDuringPlay]
    6. #if UNITY_2018_3_OR_NEWER
    7. [ExecuteAlways]
    8. #else
    9. [ExecuteInEditMode]
    10. #endif
    11. public class CinemachineTerrainHugger : CinemachineExtension
    12. {
    13.     public TerrainCollider m_Terrain;
    14.     public float m_CameraHeight = 1;
    15.  
    16.     private void OnValidate()
    17.     {
    18.         m_CameraHeight = Mathf.Max(0, m_CameraHeight);
    19.     }
    20.  
    21.     protected override void PostPipelineStageCallback(
    22.         CinemachineVirtualCameraBase vcam,
    23.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    24.     {
    25.         if (stage == CinemachineCore.Stage.Body && m_Terrain != null)
    26.         {
    27.             var b = m_Terrain.bounds;
    28.             var camPos = state.CorrectedPosition;
    29.             camPos.y = b.max.y + 1;
    30.             if (m_Terrain.Raycast(new Ray(camPos, Vector3.down), out RaycastHit hit, b.max.y - b.min.y + 2))
    31.             {
    32.                 camPos = hit.point;
    33.                 camPos.y += m_CameraHeight;
    34.                 state.PositionCorrection += camPos - state.CorrectedPosition;
    35.             }
    36.         }
    37.     }
    38. }
     
  8. VX_PM

    VX_PM

    Joined:
    Aug 16, 2017
    Posts:
    16
    Sorry for the (very) late reply, been kind of busy with other projects. Will test it as soon as I can! Thanks for the help!
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    No worries! Let me know how it goes.