Player Follow Camera Goes Inside Objects

I was working on a 3d game where the camera is continuously following the player object in position and rotation fields.

Complete 3d environment is set up as per my gameplay.
A player moves and rotates in different areas of the environment, I was getting this kind of abnormal view in front of the screen. The camera goes into the other objects.

This is a really poor gameplay experience so how to fix this?
How to make this look better when the player following the camera goes inside other environment objects?

You need to add collision to your camera and make sure that it doesn’t enter 3D objects. When I made a 3d character controller I followed this tutorial to do this with raycasting. In update you can raycast behind the player a couple meters and place the camera at the end of the ray, that way if something is behind the player the ray will hit that object and place the camera in front of the object, not inside of it.