Make Camera not go though wall

So I followed http://www.burgzergarcade.com/ and I ran into a problem that he does not fix. I need to get my cameras collider to get a message to move closer to the character when my camera hits a house.

if (gameObject.tag == "House") {
	walkDistance = 0;
} else {
	walkDistance = 30;
}

but it dose not work

I’m using C#

gameObject.tag is the tag of the GameObject to which the script is attached, nothing to do with colliders. If your Camera has a Collider, with isTrigger true, then in a script, you could write:

void OnTriggerStay(c : Collider)
{
    if (c.tag == "House") ...
}

In general, there are many different ways to keep your camera in a good location, such as:

  • Raycast from you player object back towards where you would like the camera to be, if it hits something closer, place it there.
  • Use physics: attach the camera via a damped spring to a kinematic RB at the ideal position. Be careful about rotation.
  • Keep it at a fixed instance, but change the nearClipPlane if anything is in the way.
  • Fade out or disable renderers of all obscuring objects

No solution will work well for every game. Third person camera control is very often an issue in AAA titles, so prepare to be challenged.