How do I make my camera not clip through walls?

I don’t want my camera script to rotate or do anything special because I have that handled all ready. All I want is my camera’s position to scale depending on how close it is to the wall.

How I want my camera script to work in 2 ways:

  1. I want my Camera to change its position based on what surface it collided with(I have this accomplished)

  2. I want my camera to scale its position based on how closely it is to the wall(Main problem).

My scene:

My script(the bit with /* is my attempt at the problem)

   using UnityEngine;
   using System.Collections;
    
    public class CameraScript : MonoBehaviour
    {
    
        public int count;
        private Vector3 Campos;
        public Transform player;
        public GameObject Player;
        public int RayDistance = 10;
        [HideInInspector]
        public GameObject Wall;
    
        public void Start()
        {
         //   if (Campos == null)
           // {
                Campos = new Vector3(0f, 5f, -15f);
           // }
        }
    
        void FixedUpdate()
        {
            //if (connection)
            
                transform.position = Player.transform.position + Campos;
                transform.LookAt(player);
            
    
        }
        public void Update()
        {
            /*RaycastHit hit;
            Ray LandingRay = new Ray(transform.position, Vector3.back);
    
    
            if (Physics.Raycast(LandingRay, out hit, RayDistance))
            {
               
                Wall = hit.collider.gameObject;
                float dist = Vector3.Distance(hit.point, transform.position);
                Debug.Log("distance = " + dist);
                if (dist <= 2)
                {
                    //Camera math stuff
                    //Btw tell me step by step how it works so Ik how to make a script like this in the future
                } 
            }
            */
            if (count == 0)
            {
    
    
                Campos = new Vector3(0f, 5f, -15f);
               // Debug.Log(count);
                Player.transform.eulerAngles = new Vector3(0f, 0f, 0f);
    
            }
            else
            {
                if (count == 1)
                {
    
    
                    Campos = new Vector3(15f, 5f, 0f);
                   // Debug.Log(count);
                    Player.transform.eulerAngles = new Vector3(0f, 270f, 0f);
    
                }
                else
                {
                    if (count == 2)
                    {
    
    
                        Campos = new Vector3(0f, 5f, 7f);
                        //Debug.Log(count);
                        Player.transform.eulerAngles = new Vector3(0f, 180f, 0f);
    
                    }
                    else
                    {
                        if (count == 3)
                        {
    
                            Campos = new Vector3(0f, 8f, 15);
                            //Debug.Log(count);
                            Player.transform.eulerAngles = new Vector3(0f, 90f, 0f);
    
                        }
                        else
                        {
                            if (count <= -1)
                            {
                                count = 3;
                            }
                            else
                            {
                                if (count <= 4)
                                {
                                    count = 0;
                                }
                                //the script is going to account for the ceiling position and floor position so don't worry about that
                            }
                        }
                    }
                }
            }
        }
    }

So, I just want a improvised version of my current script but I want it to scale its position based on how close it is to the wall. I’ve been trying this for a week so I am clueless on how to do this by my self…

Edit: Here is the script I forgot to mention that effects camera(sorry >_< )

using UnityEngine;
using System.Collections;

public class Dash : MonoBehaviour {

    public GameObject Previouscollided;
    private GameObject collided;
    public LayerMask Wall;
    Vector3 newPosition;

    public GameObject Wall1;
    public GameObject Wall2;
    public GameObject Wall3;
    public GameObject Wall4;
    public GameObject Floor;
    public GameObject Ceiling;
    public GameObject RecentWall;
    public int speed;


    void Start ()
    {
        newPosition = transform.position;
    }
	
	// Update is called once per frame
	void Update ()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if(Physics.Raycast(ray, out hit, Wall.value))
            {               
                collided = hit.collider.gameObject;
                RecentWall = collided;
                //Debug.Log(collided);
                Cameraposition();
                collided.layer = LayerMask.NameToLayer("Ignore Raycast");
                newPosition = hit.point;                   
                 Previouscollided.layer = LayerMask.NameToLayer("Wall");
                Previouscollided = hit.collider.gameObject;
                //Debug.Log(Previouscollided);
            }
        }

        if (collided == Wall1)
        {
            transform.position = Vector3.Lerp(transform.position, newPosition + new Vector3(0, 0, -2f), speed * Time.deltaTime);
        }

        if (collided == Wall2)
        {
            transform.position = Vector3.Lerp(transform.position, newPosition + new Vector3(0, 0, 2), speed * Time.deltaTime);
        }

        if (collided == Wall3)
        {
            transform.position = Vector3.Lerp(transform.position, newPosition + new Vector3(-2, 0, 0), speed * Time.deltaTime);
        }

        if (collided == Wall4)
        {
            transform.position = Vector3.Lerp(transform.position, newPosition + new Vector3(2, 0, 0), speed * Time.deltaTime);
        }

        if (collided == Floor)
        {
            transform.position = Vector3.Lerp(transform.position, newPosition - new Vector3(0, 0, 0), speed * Time.deltaTime);
        }

        if (collided == Ceiling)
        {
            transform.position = Vector3.Lerp(transform.position, newPosition - new Vector3(0, 0, 0), speed * Time.deltaTime);
        }
    }
    public void Cameraposition()
    {
        if(collided == Wall1)
        {
            GameObject theCamera = GameObject.Find("Main Camera");
            CameraScript cameraScript = theCamera.GetComponent<CameraScript>();
            cameraScript.count = 0;
        }
        if (collided == Wall2)
        {
            GameObject theCamera = GameObject.Find("Main Camera");
            CameraScript cameraScript = theCamera.GetComponent<CameraScript>();
            cameraScript.count = 1;
        }
        if (collided == Wall3)
        {
            GameObject theCamera = GameObject.Find("Main Camera");
            CameraScript cameraScript = theCamera.GetComponent<CameraScript>();
            cameraScript.count = 2;
        }
        if (collided == Wall4)
        {
            GameObject theCamera = GameObject.Find("Main Camera");
            CameraScript cameraScript = theCamera.GetComponent<CameraScript>();
            cameraScript.count = 3;
        }
        if (collided == Floor)
        {
            GameObject theCamera = GameObject.Find("Main Camera");
            CameraScript cameraScript = theCamera.GetComponent<CameraScript>();
            cameraScript.count = 4;
        }
        if (collided == Ceiling)
        {
            GameObject theCamera = GameObject.Find("Main Camera");
            CameraScript cameraScript = theCamera.GetComponent<CameraScript>();
            cameraScript.count = 5;
        }
    }
}

@dragonking300 You can improve this by using Physics.Linecast , it can help you to detect anything coming between cube and camera. Linecasting from camera will help you detect any hurdles between target and camera then you can translate your camera position according to detection.

If you put colliders on your walls you could do something like this:

//inside camera class
Vector3 CalculateCameraPosition(float maxLerpDist = -1f)
{
   Vector3 result = transform.position;
   //here put a ratio between 0 and 1.
   //0.8 means the camera will be place at 80% of the distance between the player and the wall.
   float ratio = 0.8f; 
   //We start a ray cast originating from the player position, in the direction facing the back of the camera
   Ray ray = new Ray(player.transform.position, -transform.forward);
   RaycastHit rayHit;
   //Looking for the wall behind the camera
   if(Physics.Raycast(ray, out rayHit))
   {
      //We calculate the distance between the player and the intersecting wall behind the camera and get 80% of the distance by multiplying the ratio.
      float distance = (Player.transform.position - rayHit.point).magnitude * ratio;
      //If we do not set a maxLerpDist or if the maxLerpDist is set and superior to the distance.
      if(maxLerpDist < 0 || distance < maxLerpDist)
      {
          //we place the result at 80% of the distance between the player and the wall
          result = Vector3.Lerp(Player.transform.position, rayHit.point, ratio);
      }
   }

   return result;
}