I have seen a couple questions on similar subjects but none of the answers am I satisfied with.

I have a camera which follows a game object; the problem is that it can pass through walls and because of this, has the potential to show some areas that aren't meant to be seen.

I have my walls built using a mesh exported by Sketchup. So there aren't clean barriers that I can write in code. I'd much rather do it using the same physics system that correctly prevents my player object from moving through the wall.

Yet this doesn't seem to work. I try to put a collider on the camera, and also a rigid body, but it has no effect.


here is a image of my camera's collider bounds - see how it is partly inside the wall?

enter image description here

The walls are imported from FBX and I generated colliders upon import, so it's a mesh collider.

I am using cinemachine for the camera script.

  • \$\begingroup\$ Do your walls also have colliders? "I try to put a collider on the camera, and also a rigid body, but it has no effect", do you mean the camera's collider is not colliding with the walls? Because it must collide, and that will be very close to call it as a solution. \$\endgroup\$
    – Tolga Şen
    Aug 20 '17 at 10:27
  • \$\begingroup\$ @TolgaŞen I imported the walls as FBX files from Sketchup. While doing so I checked the "generate colliders" and now there's a mesh collider on them. \$\endgroup\$ Aug 20 '17 at 16:57
  • \$\begingroup\$ "I have seen questions on similar subjects but none of the answers am I satisfied with" - to ensure we don't give you similarly unsatisfactory answers, it would help if you could explain what specifically about the previous answers didn't meet your needs. We know camera collision is possible - countless games have done it - it's just a matter of finding what methods are best for your game's specific needs. Sticky Collision Beam and 50 Camera Mistakes have good examples \$\endgroup\$
    – DMGregory
    Aug 20 '17 at 19:20

Perform a linecast from the player to your default camera position. When you have a collision, move the camera to the point of the collision. When there is none, move the camera back to the default position.

This requires that all objects which are supposed to block the camera have a collider. If you would like to have objects with colliders which affect either the gameplay or the camera but not both, put these on different layers and use the layer masks flags.

Please note that this is just a very basic solution. Good 3rd person cameras are tricky, and many good games got ruined by bad camera handling. But the "best" solution depends on the game, because you will have to make tradeoffs between practical, aesthetic and ergonomic concerns.

  • 1
    \$\begingroup\$ Does linecast guarantee that the returned hit will be the closest intersection to the start point, the way raycast does? It wasn't immediately clear to me from the documentation. \$\endgroup\$
    – DMGregory
    Aug 20 '17 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.