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
- Load the 3d game kit
- Place a vertical plane in the scene and set it to a layer the CinemachineCollider will collide with.
- Walk the player next to the wall and shift the mouse causing the camera to jump.
- Set the damping and damping while occluded to 2, repeat step 3.