Help with camera hitting walls

Hi all,

So I have an interpretation of the SmoothFollow script but written in C#

I have added a Ray to the back of the camera and at the moment the camera will hit the wall and the camera will stop. The car will then reverse untill it hits the wall.

When I drive off, the camera stays where it is because of the Ray
How can I get the camera to carry on following the car when it moves off again?

Thank you

void Update ()
{
	float wantedRotationAngle = target.eulerAngles.y;
	float wantedHeight = target.position.y + height;
	float currentRotationAngle = transform.eulerAngles.y;
	float currentHeight = transform.position.y;
		
	if (!target)
	return;
		
	Vector3 back = transform.TransformDirection(Vector3.back);
	Debug.DrawRay(transform.position, back * 1, Color.green);
	if (Physics.Raycast(transform.position, back, 1))
		{
			Debug.DrawRay(transform.position, back * 1, Color.black);

		} else
		
			currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 		
			currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 
			Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

			transform.position = target.position;
			transform.position -= currentRotation * Vector3.forward * distance;
 
			transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);

			transform.LookAt (target);
                     }
	}
}

First of all: After the ray hits the wall, the code to position the camera will never be called again. So the ray won’t move and will continue to intersect the wall. Instead calculate the new position of the camera into a variable, make your raycast from that position and then move the camera to the new position, if the raycast doesn’t hit the wall.
Is there a reason, you can’t use a collider on the camera?

Thank you for the reply, I will give it a go.

I have no idea, I have tried using a collider on the camera but it just doesn’t work. All my walls are setup correctly with colliders to.