Camera Problem for my 3rd person game

Hey Guys i am working on a RPG and i noticed while i was testing my game,when i rotate my player and if there is a wall behind him the camera will go trough it. Is there a line of code that can prevent this from happening please let me know.

skivacs1 gave a good answer, just thought I'll add a simple code as an example... If I remember correctly this next code piece is from the 3rd person platformer tutorial:

public var lineOfSightMask : LayerMask;
function AdjustLineOfSight (v3NewPosition : Vector3, v3Target : Vector3): Vector3
{
    var hit : RaycastHit;
    if (Physics.Linecast (v3Target, v3NewPosition, hit, lineOfSightMask.value))
        return hit.point;
    return v3NewPosition;
}

This is run after the new position of the camera is calculated. v3NewPosition is the position the camera will be this frame. v3Target is the position of the player. lineOfSightMask should be set to the layers you want to raycast against (if you want the camera to avoid ALL colliders in the scene - set this to "Everything"). This allows you to create a layer for objects that can potentially block the view, yet make the camera pass through others if necessary.

It's pretty simple, but does the job pretty well. If moving the camera to the new position will block the line of sight - this will adjust the position so the camera will move closer to the player as needed.

That's because your camera is not colliding with anything. You would need to implement camera collision.

This topic is discussed at length in various locations. No solution is a perfect one-size-fits-all solution, so you'd best decide what solution best fits your usage. Solutions include the use of Untiy's physics with rigidbodies, raycasting, spherecasting, or some manual setup dependent upon your scene. A quick search for camera coliisions or collision will yield some useful results.

You will need colliders on anything that the camera is to collide with. A script can then check for collisions by sphere/ray cast and move some offset from the collision(s) or using a sphere collider and rigidbody on the camera could work too, but will almost never look right and will likely be difficult to work with. You could also set something up specific to your scene to always move your camera x distance from all objects of type y within radius z, but that's fairly specific and makes some assumptions.