- Home /
Can I make mass and density constant independent of Collider 2D size?
I am currently messing around with 2D buoyancy effectors for a sprite based game. I have made a swimming animation for the player object that changes the size of the collider boundaries frame by frame. It seems as though the buoyancy calculation in Unity finds the density of the object using Rigidbody.mass / Collider2D.size (or something like that). I guess this makes sense for most cases, but because of the animation this constantly changes the density for the player object causing the player to weirdly float or sink with each animation frame. The "real" size of the object should not actually be changing so it would be nice if I could simply tell Unity to use a constant mass and density and just have it ignore the size of the collider. I looked at Rigidbody2D.useAutoMass, but that would change the object's mass with each frame, which is also not what I want. Is there a way to do this, or else a better way to handle buoyancy for an animated object?
Answer by ReticentDaikaiju · Jul 04, 2017 at 05:41 PM
After some thought I've figured out a simple solution. I can separate the calculated mass/density from the collision detection by simply making two colliders and using one for each task. The way I did this was as follows:
Select useAutoMass in the rigidbody so that I can directly set the density of each collider.
Set the density of the already existing animated collider to 0 so that it does not affect the mass of the rigidbody. This is the collider to be used for the actual collision detection.
Add a second non-animated collider. I can now freely set the size and density of this collider to give me the overall mass and density I want for the rigidbody.
The mass and density is now independent of the size of the animated collider. As long as the non-animated collider is always smaller than the animated collider so that it never actually influences collision detection everything seems to run okay.