- Home /
Static Colliders in the 2D Engine in Unity 5
So in Unity 5, there was a change to how static colliders work in the 3D Engine, the patch notes reading
Moving static colliders does not cause performance penalty anymore.
But because 2D physics use a separate engine, does moving (or in my case changing them by switching isTrigger on and off) static colliders in 2D still cause performance issues?
And to address my issue of toggling isTrigger on and off, should I use a kinematic Rigidbody? (Assuming changing static colliders still have performance issues) As I have a lot of these colliders, so if I don't need the extra Rigidbody calculations, that would be great.
Answer by MelvMay · Dec 22, 2015 at 10:02 AM
I implemented the 2D physics in Unity and one thing I really don't like is that not adding a Rigidbody2D means all colliders are static. This gives the false impression that they are somehow better without one and that adding one is worse.
The actual fact is that by not adding a Rigidbody2D component (which just attaches the colliders to a dynamic/kinematic body), the colliders are instead adding to a static body which is created implicitly. In other words, colliders are always added to a body.
Don't move a static collider, it has a penalty in 2D; attached it to a Kinematic body.
Toggling trigger on/off doesn't really have a bearing on the above. Know that like many other properties you change on colliders/rigidbody, the collider/rigidbody is being recreated completely behind the scenes; Box2D is mostly immutable in that it allows you to set things up but not change them; only the obvious things are allowed to change. Changing a collider IsTrigger causes a recreation (it's called a sensor in Box2D).
Would creating (Instantiating) a new collider and then destroying it be faster/more efficient than toggling isTrigger on and off?
Also, these are PolygonCollider2Ds, does that make a difference with it being immutable?
It wouldn't be faster no. $$anonymous$$odifying PolygonCollider2D can be very expensive because it's not a primitive collider, it's an outline (or outlines) decomposed into multiple primitives, all of which have to be recreated.
Really don't understand why you'd want something to be a trigger then not a trigger.
If you just don't want it to collide with anything then the absolute fastest way is to use: Rigidbody2D.simulated which just deactivates the body and its colliders, joints and contacts. Doing this doesn't require recreating colliders, it's just internally ignored inside Box2D.
Your answer
Follow this Question
Related Questions
(2D) Can't use colliders when I have a kinematic rigidbody? 1 Answer
Tilemap Collider 2D preventing objects from moving 2 Answers
Polygon Collider 2D doesn't have the "edit collider" button avaliable. 0 Answers
CircleCollider2D vs PolygonCollider2D: which collision solving is less expensive? 1 Answer
Moving colliders that are part of a composite collider 1 Answer