Preventing camera clipping

Hello,
I wondering if anyone found a good way to prevent camera from going through walls in tight areas? I went through tens of tutorials and threads but the most common method seems to be casting ray to see if camera is obstructed by something, the problem with this is every tree or small object that will obstruct the camera makes it jump to the player.

I recorded this video

From this asset demo

Which is basically Buzz3d 3rd person tutorial

anyway you can see how camera jumps every time tree gets in the way. Now one thing I could do is introduce layers for raycast to ignore the trees etc, but that is kind of a pain in the long term for environment artists.

One other method I found was from this tutorial series

where he kind of checks vector intersection point between camera and player and move camera on X and Z to that vector. But he still have collision issues in the corners.

I stumbled upon Unreal Engine tutorials and they had this spring physics system, where camera had collision and was spring attached to player and if camera collided with something spring would contract. Would this work for unity? Have any of you guys tried this?

Or if you have any solutions that works well work 3rd person camera that would be awesome!

Thanks in advance

You could add tags to everything that should jump the camera, or perhaps if easier add tags to the things that should not jump the camera.

Another thing is to just keep the camera really close when in tight space. If you are bothered with the visibility of your player you can try lowering the opacity on the materials of the meshes between the camera and the player.

@sotha_sil A way to avoid the jumping is to make the camera move slowly only on the way out. This means that when the raycast hits an obstacle the camera moves immediately forward (when it collides with the tree), but when the raycast stops detecting an obstacle, instead of instantly returning to the max distance the camera is set to, you slowly interpolate it, this kinds of smoothes the camera movements, especially in enclosed spaces.

Also, in order to avoid wall clipping when the character is too close to walls consider using spherecast instead of raycast, a spherecast with a big enough radius will make it impossible for the nearClipPlane of the camera to “half traverse” a wall.

Finally I would strongly recommend to thourougly check the protectFromWallClipping script that comes with the standard assets (or just use it), I haven’t found any camera tutorials better than this about the subject (for my coding level at least).