Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Cinemachine does not respect collider extension when combined with freelook

Discussion in 'Cinemachine' started by awiebe, Apr 19, 2021.

  1. awiebe

    awiebe

    Joined:
    Oct 16, 2018
    Posts:
    13
    I've been trying to combine the CinemachineFreeLook VirtualCamera with the Cinemachine collider extension to enhance the rig that came with the 3D game kit. It does not behave as expected as it passes through walls and is also quite erratic in tight spaces.

    What I Want
    If I am using a freelook camera and the result of my input would be pushing the camera through a wall, then Cinemachine should prioritize collision avoidance and ignore my camera input on the offending axis for that frame. At a minimum the cinemachine collider should be doing this, right now it will push through the wall. What follows is other things that are needed by the cinemachine collider to make sense in freelook.

    Additionally right now a camera can only preserve height or distance, a behavior which is disorienting when using freelook. A third mode is needed that tries to preserve distance and then height by pivoting instead of zooming. Ideally the Camera should be repelled by the wall to maintain distance and when possible height, as though on a rigid boom at a fixed pivot distance from the follow target. If the camera radius is being pressed against a surface while being dragged by the follow distance, it should attempt to pivot around follow target to avoid the obstruction, this can take on two behaviors.

    If pivoting around lookAtHeading.up, both height and distance can often be preserved, because the obstruction has flanked the camera from left or right but there is not a ceiling in the way.

    If a ceiling creates the obstruction then the camera needs to be pivoted around lookAtHeading.right, this preserves distance by sacrificing height.

    By analogy imagine you stuck the camera on a very long selfie stick and had to maneuver it through a corridor.


    Right now if the lookat gets into a tight spot the camera avoidance can be quite sporadic, because it does not attempt to maintain composer orbits in the same manner as the player inputs.

    Looking at the code it looks like it tries to do a form of recursive ray bounce based repositions that obey distance(by calling the zoom strategy). Basically this adds one more step before that where you see if you can pivot horizontally(since you want to preseve height) and then vertically if still needed(to preserve distance), and then you can fall back to the other strategies.

    What Happens
    Pushing the Cinemachine CameraX causes the Cinemachine camera to penetrate the wall and then try to de-penetrate it, rather than smoothly sliding along it. Setting the damping too low causes it to jump erratically, setting the damping up causes it to slide through the wall and render, then gradually slide back to a reasonable position, but it rendered behind the wall which defeats the purpose.

    Reproducing the issue
    1. Load the 3d game kit
    2. Place a vertical plane in the scene and set it to a layer the CinemachineCollider will collide with.
    3. Walk the player next to the wall and shift the mouse causing the camera to jump.
    4. Set the damping and damping while occluded to 2, repeat step 3.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,141
    Have you tried replacing the FreeLook camera with a 3rdPersonFollow rig? See the AimingRig sample scene for an example of this. We think it's much better suited than FreeLook for this kind of camera. It has built-in collision resolution, so you don't need to use the CinemachineCollider.