- Home /
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.
Can collision detection be added as a component to a camera?
Answer by bartleycollin · Jun 13, 2013 at 05:00 PM
You could do 2 things.
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
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!
This solution does not work anymore. You need a Rigidbody on the parent as well (turn off gravity) and move it with Rigidbody.$$anonymous$$ove()
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
Cast 4 rays from player to camera's near clipping plane. If the rays hit an obstacle, move the camera towards player until no collision detected. Can do a lot with this but that's a rough idea of it.
Your answer
Follow this Question
Related Questions
Having an array of points(vector2), how do I create a 2d terrain? (new to unity) 2 Answers
Combine common surfaces 1 Answer
Camera collision 2 Answers
Scale Vertices propotional to Camera Frustum 0 Answers
Plane is not rendered when look up 1 Answer