Unity3D: Troubleshooting Rigidbody problems
Posted by Dimitri | Jan 7th, 2011 | Filed under Programming
Maybe the component that give Unity3D developers the most number of headaches is the Rigidbody. Sometimes it doesn’t seem to not work the way it’s supposed to, although these problems are mostly caused by wrong parameter values and settings. That’s the main reason behind this post: to point out the most common problems encountered when using rigidbodies in Unity3D.
Before listing these problems, keep in mind that the Rigidbody is part of the physics simulation in Unity3D which is decoupled from the game logic, and that’s why there is a separated Physics Time Step and a different method dedicated to it- the FixedUpdate() method. And before trying to figure out what the problem is, check the basics and see if all GameObjects the Rigidbody collides with have an attached Collider component, or if some parameter is set too high or too low.
Below, there is a list of the most common Rigidbody component problems and how to fix them:
1. Gameobjects are interpenetrating each other
That may be caused by the Min Penetration for Penalty value if it is set too high for your game. To change it, go to: Edit -> Project Settings -> Physics and set the Min Penetration for Penalty to a lower value:
Alternatively, this can be caused by the Sleep Velocity and the Sleep Angular Velocity values: they may be too low. These velocities determine whether the Physics engine can stop calculating the collisions, forces and friction to a given Rigidbody. Any velocity below these values puts the Rigidbody into the sleep state, as it is ignored by the next physics engine update, to save precious resources. Maybe the Rigidbody is entering this state too early in your game. These values can also be changed at the Physics Manager.
2. Gameobject with attached Rigidbody can’t be controlled or moved by any script
The main problem here is that the isKinematic flag is set to false. When enabled, this property makes the physical simulation stop for that specific Rigidbody, so it is only possible to move it using a script. This parameter can be changed by finding the Rigidbody component in the inspector, and checking the isKinematic check box:
Alternatively, if you want to have control over the isKinematic property using code, insert this code at the initialization of a script that is attached to your gameobject:
rigidbody.isKinematic = true;
3. Controllable Character totally/partially ignores the Rigidbody
Another problem that can be caused by the sleep velocity. Maybe it is set too low for your character, and he collides with the Rigidbody when it is sleeping. Go to: Edit -> Project -> Settings -> Physics and set the sleep velocities there. The problem can also be caused by the collision detection method Unity3D is currently using. Place this code in a script that is attached to the Rigidbody:
rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
As stated at the official documentation, this can have a big impact on performance so use it wisely. Another cause to this problem is the Rigidbody rotation. To freeze it, read the instructions on item 5.
4. Rigidbody needs to be controlled through user input, but Unity3D doesn’t allow the Rigidbody and Character Controller components in the same Gameobject.
Don’t use the Character Controller component. Instead, attach a Rigidbody component to the gameobject and take a look at this script: PhysicsFPSWalker. It’s a good starting point. This script will push and pull the gameobject based on keyboard input.
5. Controllable character can’t stand on top of a Rigidbody or can’t use it as a platform
The most common cause of this problem is the rotation of the Rigidbody, just set the property freezeRotation to true, like this:
rigidbody.freezeRotation = true;
Or go to the Rigidbody component and set the Freeze Rotation check box to true:
In the case of a moving platform, maybe setting the Rigidbody isKinematic property to false can help fix the problem. See item 2.
None of the tips listed above worked!
Maybe there are too many GameObjects with the Rigidbody component in your scene. Try to use the Rigidbody only on GameObjects that really need it. For the rest, use just the Collider component – it will avoid a lot of unnecessary physic calculations, the scene will get much more simpler to edit and it will be easier see what’s going on.
Just in case that isn’t enough, maybe the object you expecting to behave as a physical object can’t just enter the sleep state. So put this code in your Update() method and it will tell the Rigidbody to wake up every frame:
rigidbody.WakeUp();
It is extremely not recommended to do so, try to use this instead:
rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
Before resorting to the WakeUp() method. Again, this can affect your game’s performance.
And that’s it!
Isn’t there a problem with the PhysicsFPSWalker link?
Try this one: http://www.unifycommunity.com/wiki/index.php?title=PhysicsFPSWalker
Yes, there was! Thanks! It has been fixed. It’s now working normally.
Turnrd out my problem was the last “if it still doesn’t work” suggestion – too many rigidbodies in the scene.
Hi,
your blog is really cool!
I’m creating a physics simulation with the boxs, but still have the problem that the collider penetrate between them.
So I have a unit of measurement box collider scales (1,1,1), and as the father of Object game with a rigidbody that form different pieces.
I have created a Game Object gravity and form a wall that expands to Y, every now and then if the load is heavy boxes penetrate each other trembling ..
setting the parameters that I read on the blog, the situation is resolved a bit but the tremors are over when the load is higher than the bottom ..
What can I do to prevent it from entering the collider boxs?
I can not find a solution ..
I utilized this: # http://wiki.unity3d.com/index.php?title=DontGoThroughThings C.23_-_DontGoThroughThings.js
but without success
Richard