RigidBody's Center Of Mass doesn't always update properly
Hi everybody !
I'm struggling with a physics behavior that is not supposed to be the one mentioned in the Scripting API documentation : The doc says that if you don't manually set the Center Of Mass of a rigid body, the COM will be updated each time a modification occurs on the rigid body (such as removing colliders). But in my game, I sometimes need to remove (unparent) colliders from a hierarchy at runtime, and SOMETIMES the COM isn't recalculated properly ...
So I tried to call ResetCenterOfMass()
each time I remove a collider (in order to be sure to recalculate the COM), but even with that I still have COMs that aren't properly reset after a collider removal :/
It's weird because it seems pretty random, so I was wondering if there is constraints that I'm not aware of, when it comes to change the structure of a compound collider ? (like for instance : "only remove colliders in a fixed update" or any other hidden information like this ^^)
Any clue ?
Answer by saharolds · Dec 19, 2019 at 12:12 PM
I just had this same problem. I tried to reset the center of mass after removing a collider and it usually wouldn't work. Here is a very simplified version of what I was doing (the collider I wanted to remove was part of a child object):
Rigidbody rb = GetComponent<Rigidbody>();
Destroy(transform.GetChild(0).gameObject);
rb.ResetCenterOfMass();
rb.ResetInertiaTensor();
Since the Destroy() function is often delayed in execution, I believe the center of mass was recalculated before the collider was actually destroyed. To fix this, I simply disabled the collider before destroying it and resetting the center of mass:
Rigidbody rb = GetComponent<Rigidbody>();
transform.GetChild(0).GetComponent<Collider>().enabled = false;
Destroy(transform.GetChild(0).gameObject);
rb.ResetCenterOfMass();
rb.ResetInertiaTensor();
Disabling the collider is not delayed in execution and thus it allows the center of mass to be recalculated properly. This solution seems to work in all the examples I've tried so far.
Your answer
![](https://koobas.hobune.stream/wayback/20220612190049im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Case of the Haunted Standard Car Assets? 0 Answers
transform.addforce to stay in local z axis 0 Answers
Can you point me in the direction of how to get space physics like the one in this video? 1 Answer
Raycast detecting colliders issue w/ image depicting problem 0 Answers
Changing direction of a hing joint 1 Answer