- Home /
BoxCollider2D + Edge radius + use auto mass
Hi
In Unity 5.6, we can now add an edge radius to the BoxCollider2D, the performance on my mobile is similar with or without the edge radius and this is exactly what I need for my game. So now I want it ! ;)
To respect the shape, I need to reduce the size of the default box collider.
The problem is when the Rigidbody2D has "use auto mass" to true, it does not consider the collider with radius.
See in picture :
According to the documentation for the edge radius "Controls a radius around edges, so that vertices are circular. This results in a larger Collider 2D with rounded convex corners." So I expected that the Rigidbody use the larger box collider to calculate the mass.
So my questions are:
Is that the expected behaviour ? Or the Unity's team has forget something about this feature and I can report a bug.
What is the best workaround to get a mass close to 1 with auto mass + edge radius ? (Im working on a script that disable auto mass and calculate the mass according to the bigger collider)
Thanks for the help !
Answer by MelvMay · Apr 06, 2017 at 07:38 PM
Documentation is your friend.
https://docs.unity3d.com/ScriptReference/BoxCollider2D-edgeRadius.html
It is important to know that when using Rigidbody2D.useAutoMass, changing the edge radius does not change the calculated Rigidbody2D.mass even though the collision area has changed. The mass is calculated as if the edge radius is zero i.e.not used.
If you absolutely must do this and must use auto-mass then your only option is to reduce the density of the collider although I do not understand why you'd want to use auto-mass then set the mass to 1.
You're right, I've missed this point in documentation. In fact, I have a lot of rigidbodies that use the auto mass option but the mass with edge radius change the behavior of my game.
$$anonymous$$y workaround is to use script like that :
BoxCollider2D bc2D = GetCompent<BoxCollider2D >();
if(bc2D.edgeRadius>0f)
bc2D.density *= ((bc2D.size.x + bc2D.edgeRadius * 2f) * (bc2D.size.y + bc2D.edgeRadius * 2f)) / (bc2D.size.x * bc2D.size.y);
That change the density, but I get my initial mass back ;)
Thanks a lot for your help !
Your answer
Follow this Question
Related Questions
Why collision2d happend earlier in rigidboy2d? 2 Answers
2D Box Colliders Overlapping On Collision 4 Answers
What's a good way at keeping a ball bounce around inside the scene? 1 Answer
Why does poligon collider 2d restricts movement of box collider? 0 Answers
Ball bounces at different heights when it shouldn't 0 Answers