- Home /
Prevent player from walking through/pushing walls that are Rigidbodies and have Box colliders
I have a player ( camera object ) in Virtual Reality Environment. The player is in a room with objects like a table and walls. The tables and walls both have a rigidbody and a box collider on them. I messed with the player adding a rigidbody and capsule collider, but I end up either walking through walls, pushing walls, or flopping over as a result of running into the table. I set the walls and table's Rigidbody mass to max 1e7?
How do I make it so that the player simply cannot walk through the table nor walk through the walls without pushing each other?
If you don't need gravity on the walls or table, etc. under the Rigidbody settings you can check Is $$anonymous$$inemetic and uncheck Use Gravity. This will essentially make them static unmovable objects in the scene.
Also, make sure you are moving the rigidbody, not the transform.
$$anonymous$$y script moves the camera transform(Virtual reality Google cardboard camera head) so is there a way to work around it? Does transform always walk through other objects?
So if my player is the camera object do i need a 3D Capsule as the camera's parent or can i just put a box collider(make the collider like a vertical rectangle) and attach a rigidbody? The walls i can put rigidbody, is $$anonymous$$inematic, no Gravity.
A camera object with a box collider and a rigidbody is pretty much the same as a cube with a box collider and a rigidbody and a child camera object, except it has a mesh renderer. Personally I'd probably make a 3d capsule gameobject and make the camera object it's child.
You could perhaps move the rigidbody by changing the script to this: if (isWalking) { lVel = transform.InverseTransformDirection(GetComponent().velocity); lVel[2] = speed; //or any direction that is forward in local position from the player GetComponent().velocity = transform.TransformDirection(lVel); }
Answer by ybaklaci · Oct 12, 2017 at 10:48 AM
You don't need a rigidbody to stop your character from walking through walls. The OVR utility from Oculus has a Player Controller (with a CharacterController component) which handles this. If you want to achieve it by yourself, you can use a Capsule collider (CharacterController already has it) and stop the transfrom on a collision.
But, if your aim is also to stop the Camera from looking through objetcs, that's a bit tricky. Keep in mind that CharacterController is moved by using buttons on the remote, or by some kind of game controller. So, it is easy just to stop reacting to the input by the user. What you can't stop, is the positional tracking of the headset, which is strictly synced with the Camera.
As a fast solution to all of these, there is an asset called Head Collision VR. I also refer to the problem of head collision in depth in this answer.