how to prevent camera from moving through meshes

How do you set up the camera or scene or mesh objects to prevent a camera from moving through the meshes? Would be better if the camera position is prevented from passing through mesh volumes.
Thanks,
Kevin.

Use collision detection.

Can collision detection be added as a component to a camera?

3 Answers

3

You could do 2 things.

  1. If you meant that when the camera bumps up against a wall, you can see through the wall, then do this. Select the camera, go the inspector and set the near clipping planes to 0.1

  2. If you meant that you wanted the camera to be able to bump against a wall, without actually going through the wall, then create an empty game object. Give the empty a box collider, and set it to the exact location of the camera. Finally set the empty object as the Camera’s parent. Now when you move you Empty, it moves the camera, and it won’t go through the walls. Hope this helped!

You are awesome.. Thank you so so so much......

This solution does not work anymore. You need a Rigidbody on the parent as well (turn off gravity) and move it with Rigidbody.Move()

Great solutions, thanks!

Dude, #2 was a brilliant solution

This kinda works but it causes problems. If the camera is in third person, once the camera hits a wall, it prevents the player from moving further back. But when moving the mouse camera direction while moving, you can get the camera to clip anyway. And when the camera collider collides into another object, Unity crashes. Anyone have a better solution? Like maybe the camera will move closer to the player when squished between the player and the wall and retract back when that space h

Great solutions, thanks!

Dude, #2 was a brilliant solution