- Home /
Rigidbody (with Box Collider) falls through floor
Hej fellow Unity users!
As stated in the title of my question I got a problem with rigidbodies falling through the floor. I've done my homework and googled for it but the most answers were "turn isTrigger off" and "don't user mesh colliders". As seen on the picture below my bullets seem to rest on a plain inside my solid floor that has a box colider attached to it (that is not a trigger and is 2 units thick).
I spawn my bullets via code and attach a rigidbody and a box collider to them at runtime in a update function (could that be an issue?)
I'm often struggling with the phsysics in Unity, seems we two got some problem, hope you guys can give me some hints.
The Code that spawns the shell:
public void SpawnShell() {
GameObject go = GameObject.Instantiate(ShellPrefab, ReferencePosition.position, Quaternion.identity) as GameObject;
go.AddComponent<BoxCollider>();
go.AddComponent<Rigidbody>();
Parenting p = go.AddComponent<Parenting>();
p.Parent = "EMPTY_SHELLS";
go.transform.rotation = Random.rotation;
go.name = "Empty Shell";
//go.rigidbody.AddForce(0f, Force, 0f, ForceMode.Impulse);
//go.rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
Destroy(go, DespawnTimer);
}
EDIT: still don't have a clue. I know there have to be some inaccuracies in order to perform in a certain speed but I don't know why this doesn't work :( no one any idea?
EDIT 2: I've set the "Min Penetration For Penalty" in the Physics Project settings from 0.01 to zero. Now it looks better but not quite perfect because the box colliders are still half in the floor
Regards Mexallon
You said that you're adding the components in Update, but your code doesn't show that. Do you mean that you're calling the SpawnShell() in the Update?
If you are using 2d,then make sure that you use Rigidbody2D and BoxCollider2D and PolygonCollider2D ins$$anonymous$$d of Rigodbody, BoxCollider and $$anonymous$$eshCollider.
An easier way is to give the bullet prefab the collider and rigidbody. Then no need to add them in code, and you can adjust them in the editor (weight, drag, colliders often look better a little diff size than the object.)
Also makes it easy to position a bullet right above the ground, for a "test drop" on Play.
Answer by Mexallon · Dec 23, 2014 at 10:55 AM
I've set the "Min Penetration For Penalty" from 0.01 to 0 in "Project Settings->Physics". This seems to solve the problem more or less. As also stated in the comments I think the same way that the bullets may be to small or my physics settings are not properly.