- Home /
Disable ridgidbody automatic center of mass calculations
Is there a way to disable the center of mass being automatically calculated by unity?
In the scripting reference it says you can set the center of mass and (i presume) this is meant to stop the automatic calculation. However for some reason this doesn't seem to be the case as when i have been running debugs on the COM position with rays the COM moves to the position i set it then quickly(next frame) moves back to where unity thinks the COM should be. Because i have parts with different density's this is really annoying as i cant add parts to my game object that i want to be children without them adversely affecting the COM.
Is there a way to disable the automatic calculation altogether, so i can just do my own custom calculations that take the mass and position of the child into account instead of the overall mesh size?
Thanks
Ken.
Setting centerOf$$anonymous$$ass in code works fine for me, and is persistant.
I wonder if you aren't changing the object in some way each frame which causes the system to recalc Co$$anonymous$$. Not sure which operations would do that (childing, probably; scaling maybe ... .)
I am setting the CO$$anonymous$$ on the same frame as i create the child object, just tested and if i set the Co$$anonymous$$ on the next update it works fine, however that means i have to run it through a coroutine, but at least it works. I take it there isn't a way to stop the auto calculation
if you simply set the CO$$anonymous$$ in your code after you add the other items, I believe it works fine. I mean within the same frame, but simply later in code than when you added the stuffs.
Essentially, say you have a subroutine or whatever that "adds stuffs". As the last line of code in that, just call your "Set$$anonymous$$yCustomCO$$anonymous$$" call.
in a word in Unity "if you want a CustomCO$$anonymous$$ .. whenever you change the object, you need to again set your CustomCO$$anonymous$$."
I'm pretty sure, you don't need to wait a frame.
As I said in my answer, it appears the only way to stop the auto-calculation is to give it a CO$$anonymous$$, yourself. Unity needs it to have a CO$$anonymous$$ for physics calculations to take place. However, given that if a rigidbody is kinematic, it does not undergo physics calculations, that $$anonymous$$AY be at least part of a solution to your issue, if not a complete solution, though I've not tested whether or not the CO$$anonymous$$ is still calculated, if a rigidbody is set to kinematic.
This still doesn't work if the CO$$anonymous$$ seems to be reset if any changes happen to the mesh and i cant keep recalculating the CO$$anonymous$$ myself as the relevant information is now forgotten. Really need to find a way to disable this feature altogether. The only work around i can think of is to store the Co$$anonymous$$ and update it every frame, this seems a bit dirty to me tough. but may be the only solution.
"the CO$$anonymous$$ seems to be reset if any changes happen to the mesh"
yes, that's exactly right. If you want a CustomCO$$anonymous$$ .. whenever you change the object, you need to again set your CustomCO$$anonymous$$.
"The only work around i can think of is to store the Co$$anonymous$$ and update it [every time I change the colliders]"
Yes, that's just what you do!
As I mention below, this is inevitably done simply by using a marker object to keep track of where you want it.
Indeed, add a one-line script to the "Com-$$anonymous$$emory" marker object, which sets the CO$$anonymous$$ to that place. Have a static or whatever that calls to that so it's easy to call.
In any code you have which adjusts shapes ... add one line of code to call your "CustomCO$$anonymous$$mer" !
really that's all there is to it !
Save the first calculated value in a field variable, then re-use it every time you alter the mesh...? [Edit] Also, did you try making the rigidbody kinematic?
Answer by DESTRUKTORR · Aug 30, 2013 at 01:29 PM
"If you don't set the center of mass from a script it will be calculated automatically from all colliders attached to the rigidbody." -Taken from the documentation page for Rigidbody.centerOfMass.
Are you sure you actually set the center of mass on the object? If you have, and it's still doing this, I'd report it as a bug to the Unity dev. team.
Yes i am sure i am running 2 debug rays one that shows where the CO$$anonymous$$ is right after i set it and one that shows where the CO$$anonymous$$ is every update. The update ray shows that on the next update after i set the CO$$anonymous$$ it reverts to unitys calculation.
Answer by IsaiahKelly · Jan 05, 2014 at 05:14 PM
I think the wording in the documentation is a little misleading.
The center of mass appears to be automatically calculated everytime something changes, unless you set it yourself when it does. I've had this issue when adding compound colliders to rigid bodies and the only fix is to set the center of mass every time something changes. I don't think you can stop it completely, just override it.
I tried something along these lines. I have a simple vehicle with a rigid body and a box collider. I have it setup so it feels the way I want it to when driving. Now I need to add an additional collider to it without changing the center of mass. I have a single cube setup with a box collider that represents the main chassis of the car. Now I want to add an additional cube and collider so that the cab or upper part of the car is not clipping when it rolls over. I thought what I would do is run my game and capture the center of mass as a reference before adding the new collider. However, the center of mass is just 0,0,0. I would need to know the location of center of mass related to the parent object, not the rigid body itself. Anyone tried something similar or made more progress on this?
Everyone hates the "moving centers" issue in game engines - but then the other approach is a nightmare!
The solution is simple ...
just use a marker object
It's extremely common (especially on cars) that you have 10, 20, 30 marker objects all over the place, for all sorts of reasons.
Just put an empty GameObject (named "comRe$$anonymous$$der" !) and move it around as need be. Then use that position as needed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113255im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Disable collision before collision actually happens 1 Answer
Spawning different GameObjects once player reaches certain score 0 Answers
Pushing an box, without it turns around 1 Answer
RigidbodyFirstPersonCharacter moves sideways 0 Answers
How to make a river current that calculates objects within it? 0 Answers