What to do to prevent the camera from going through colliders with this script

I made this script that rotate the player and the camera and also to the camera it move it a little forward (lean forward) and a little down when looking down, and a little back and up when looking up, so I can make it more realistic. The problem is that if the player is facing a wall, or something with a collider, the camera is going through it. I have already tried this to fix and didn’t worked:

  1. Place a collider at the head/camera position. It don’t work because between that collider and the body collider was a gap and the player could jump and stan in the air by standing over an edge of a mesh (is hanging by the “chin” collider, even with a sphere collider).
  2. Tried to use rigidbody.moveposition and rigidbody.position and was just acting in another way that it was supposed to do.

using System.Collections;
using UnityEngine;

public class Player_Rotation : MonoBehaviour {

[Range(1,10)]
public float lookSensitivity = 5;
private float yRotation, xRotation, currentYRotation, currentXRotation, yRotationV = 0.0f, xRotationV = 0.0f, peekingRotation = 0.0f;
[Range(0.00f,0.25f)]
public float lookSmoothDamp = 0.1f;
Transform camera, player;
Rigidbody rb;
bool rotate = true;
void Start (){camera = transform.GetChild (0).transform;player = this.transform;rb = camera.gameObject.GetComponent<Rigidbody> ();}
void Update ()
{
	if (Input.GetKeyDown (KeyCode.F1))
		rotate = !rotate;
	if (!rotate)
		return;
	yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
	xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
	if (Input.GetKey (KeyCode.Q))
		peekingRotation += lookSensitivity * 0.5f;
	else if (Input.GetKey (KeyCode.E))
		peekingRotation += -lookSensitivity * 0.5f;
	else
		if (peekingRotation > 0)
		{
			peekingRotation += -lookSensitivity * 1f;
			peekingRotation = Mathf.Clamp (peekingRotation, 0, 45);
		}
		else if (peekingRotation < 0)
		{
			peekingRotation += lookSensitivity * 1f;
			peekingRotation = Mathf.Clamp (peekingRotation, -45, 0);
		}
	peekingRotation = Mathf.Clamp (peekingRotation, -15, 15);
	xRotation = Mathf.Clamp(xRotation, -90, 90);
	currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
	currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);
	camera.localRotation = Quaternion.Euler(currentXRotation, 0, Mathf.SmoothDamp(camera.localRotation.z, peekingRotation, ref yRotationV, lookSmoothDamp));
	if (xRotation >= 0)
		camera.localPosition = new Vector3 (-peekingRotation / 25f, 1.11f - xRotation/1000f, 0 + xRotation/100f);
	else if(xRotation < 0)
		camera.localPosition = new Vector3 (-peekingRotation / 25f, 1.11f - xRotation/2000f, 0 + xRotation/1000f);
	player.rotation = Quaternion.Euler(0, currentYRotation, 0);
}

}

Your best bet may be to utilize Raycasting. you know your target camera location and your current camera location, what you can do is raycast in that direction with a max length of just farther than the distance you’ll be moving, if the raycast hits, don’t perform the movement.

Vector3 difference = (targetCameraPosition - currentCameraPosition);
Vector3 direction  = difference.normalized;
if (!Physics.Raycast(currentCameraPosition, direction, difference.magnitude + padding))
{
    //apply camera movement
}