- Home /
Camera and mesh not colliding
Hello,
I'm trying to get a camera to collide with a mesh, however at the moment I'm having difficulties getting the desired behaviour.
I am making an endless runner type game set in a tunnel. Each piece of the tunnel is comprised of sections featuring obstacles that the play must avoid to progress. To avoid these obstacles the player (camera) must rotate around tunnel.
The problem I have is that when the camera hits an obstacle in the tunnel it passes straight through it. That is unless I turn off all constraints on the rigidbody. If I do that the camera will collide with the obstacle but then a force is applied to both objects. What I would like is for the camera to collide with the obstacle, but no forces to be applied to either, the camera simply should not pass through the obstacle. This also applies to situations where the camera might need to jump onto a mesh within the level as well.
As mentioned above, if I freeze the position and rotation constraints both the camera and obstacle remain fixed on collision (no forces are applied), but the camera passes through. Both the obstacle and the camera have rigidbodies, and box and sphere colliders components respectively.
As far as I can tell this should be fairly simple, however my understanding of Unity's physics system is minimal so it's a lot more complicated than it should be!
Any assistance is greatly appreciated. Thanks.
Answer by MattBradley · Nov 13, 2013 at 01:12 AM
I've done some further investigation and found that my solution is likely to involve using a joint. At the moment I have a gameobject that controls the camera rotation at 0,0,0. The camera is a child of that object at 0,-20,0. I have a state machine that gets player input to rotate the camera around 0,0,0 with a -20 offset in the y axis.
I now have a fixed joint on the camera with the gameobject at 0,0,0 being the connected body. When I do this the obstacle no longer moves and the camera is generally in the right position unless I continue to try to rotate through the obstacle. If I do this the camera will act very strangely and not remain at 0,-20,0. Instead it rotates itself until it pings off the obstacle and loses its position.
Is there a way to fix this behaviour? I have considered turning off the ability to rotate once a collision has occurred and also to make the the camera rotate only by using forces, but both are a little scary!
Your answer
Follow this Question
Related Questions
How may I observe expected physical interactions while using Rigidbody.MoveRotation()? 1 Answer
Rigidbody doesn't rotate after collision 3 Answers
transform.Rotate and Rigidbody.MovePosition 1 Answer
How to limit the motion direction of a GameObject? 1 Answer
Camera gets flung off of the map when the player collides with certain objects. 0 Answers