- Home /
Rigidbody (Player) goes through wall
I have a game that player is the cube. That cube moves around like 2D side scrolling game. The cube has a rigidbody and box collider to it. When the player moves toward the wall, and touch the wall, a bit later, the player suddenly goes through the wall. I have searched this site for the solution that have written before, but what I found doesn't match my condition.
Here is my control script (shortcut) :
transform.Translate (Vector3(Input.GetAxis ("Horizontal") * Time.deltaTime *speed,0,0),Space.World);
Here is the link to the game if you want to see the problem more clearly.
http://www.kongregate.com/games/vitou01/cube-land
Thanks in advance.
Edit :
my wall has box collider.
actually I tried your game, but never have a case where your cube goes entirely through the wall..
actually I found a spot.... the pushed red button below opens the first falling down block, gives me the possibility to go through the wall where the button is.
by the way nice idea to make those puzzles depending on the cube's color.
@swisscoder : It goes through almost every wall. If you hold the arrow key for a long time, you will see the cube goes through the wall easily. And thx for the compliment. :D
Answer by Oninji · Jan 10, 2012 at 05:42 PM
Transform does not call the Physic components of your rigidbody, thus it ignores all physic. Colliders are physic component, for your code to include them you must move your objects using it's rigidbody physics.
http://unity3d.com/support/documentation/ScriptReference/Rigidbody.html
ie: When I do an AI that need to have perfectly controlled movement, I manage the Velocity of the rigidbody through code.
Depending on what you want to do and your ease with physics, you may want to use the Character Controller, it's essentially a rigidbody with simplified functions to be controlled as a Character.
http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=character+controller
I have tried changing the velocity of the rigidbody before. It works great. No more going through wall. However, I have trouble with control. While the cube is falling, and the button is pressed, the cube hardly want to fall. It keeps going left/right.
Here is my code I used before :
rigidbody.velocity = Vector3 (Input.GetAxis ("Horizontal") Time.deltaTime speed , 0 , 0);
Do you know how to fix this?
It's simple, the way you currently control it. You set Y and Z velocity at 0 constantly. Z being the vertical orientation, it won't fall.
Do something like rigidbody.velocity.x = Input.GetAxis("Horizontal")*Time.deltaTime*speed;
But as I stated before, if you're not entirely sure how a rigidbody works, use a CharacterController, it's the same but a bit simplified. Unless you need extremely specific controls that are unachievable with it in some RARE cases.
Horray!!!!!! Now the problem is solved! Thanks for your help! :D
Answer by swisscoder · Jan 10, 2012 at 05:14 PM
If you would add the CharacterController component to the cube, you could call it's Move method. Maybe works better. http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html
I see your camera isn't really smooth too. (Maybe you could use the methods Slerp/Lerp of Vector3, to make things smoother.)
I try the character controller. There is no more going through wall, which is a good thing. However, my cube lost its realistic. There is no more rotating.