- Home /
How does SetDensity find volume, and how can I get it?
It uses the collider to at least estimate volume (using a mesh collider here) for calculating total mass, but I can't find any way for the client (me) to access it. And it doesn't use the bounding box, I've tested.
I'm only using regular polyhedrons, and subdivisions of those polyhedrons, so calculating volume myself won't be overly complicated. Still, if volume is accessible, I'd love to have it. Depending on how Unity does it, I might also want to use more complex shapes.
Answer by robertbu · May 26, 2013 at 10:34 PM
I don't know how Unity finds volume, but you can reverse it. That is, you can do something like:
function Start () {
rigidbody.SetDensity(5.0);
Debug.Log("mass: "+rigidbody.mass);
Debug.Log("Volume: "+rigidbody.mass/5.0);
}
In a bit of play I found the mass gets set back to the default of 1.0, any time the Transform.localScale is changed. An if you do a non-uniform scale of an object, the volume will reflect how Unity scales the collider. For example, box colliders get scaled non-uniformly, but sphere colliders apparently do not.
Note in searching around for an answer, I found this post which calculates the volume of mesh (I did not test it):
http://answers.unity3d.com/questions/52664/how-would-one-calculate-a-3d-mesh-volume-in-unity.html
Ah, of course, thank you for the help! How did I not think of that...
Also, interesting behavior with localScale. I'll definitely poke around a bit more.
Your answer
![](https://koobas.hobune.stream/wayback/20220613102950im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Calculate objects' mass based on density and volume 1 Answer
Internal collisions 1 Answer
How to give dynamically add mass to a gameobject from volume(collider.bounds.size) and density 1 Answer
Volume detection. 0 Answers
Can I control mass density of colliders in a rigidbody independent of their size? 1 Answer