- Home /
How to give dynamically add mass to a gameobject from volume(collider.bounds.size) and density
I am trying to dynamically add mass to my gameobject by its size. My gameobjects are from a housing dataset, so things that may appear large for example a hollow box should not be that heavy. Any ideas
Answer by Cornelis-de-Jager · Jul 13, 2017 at 11:38 PM
To calculate the mass you need to have an density for your item. The formula for mass is
m = V * p
But you can implement it without density if say density is 1. Then it just becomes
m = V
Then all you need to do is calculate the volume and you will have the mass.
void LateUpdate () {
// Calculate mass
float volume = transform.localScale.x * transform.localScale.y * transform.localScale.z;
// Set the mass
GetComponent<RigidBody> ().mass = volume;
}
Then if you ever want to implement density then its a simple change:
GetComponent<RigidBody> ().mass = mass * density;
Your answer
Follow this Question
Related Questions
Can I control mass density of colliders in a rigidbody independent of their size? 1 Answer
Rigidbody mass not updating in inspector after calling SetDensity 2 Answers
Calculate objects' mass based on density and volume 1 Answer
How to make a controller for Rigidbody Spherical player which uses gravity?? 1 Answer