Third-person cameras can clip through walls.
Use a Raycast or SphereCast between the target and desired camera position.
if (Physics.SphereCast(
origin,
cameraRadius,
direction.normalized,
out RaycastHit hit,
direction.magnitude,
obstacles))
{
finalPosition =
origin +
direction.normalized *
Mathf.Max(
hit.distance - margin,
0f
);
}
Use a LayerMask to ignore the player and irrelevant objects.