0
\$\begingroup\$

In my scene I have very simple quadrilateral blocks, which I'm considering using PolygonCollider2D for. These blocks will be the building blocks of the scenery so there will be a lot of them, and I'd like to use the most efficient technique for them. Now, since I don't really know what kind of optimization Unity does for collisions, I was wondering: would rotating and combining several BoxCollider2Ds be more efficient? And is a quadrilateral BoxCollider2D even worse than a rotated BoxCollider2D (which from a mathematical point of view wouldn't be able to apply an AABB collision scheme either) in the first place?

\$\endgroup\$
  • \$\begingroup\$ "These blocks will be the building blocks of the scenery so there will be a lot of them" This sounds like a recipe for trouble. I know a lot of games look like they're made out of hundreds or thousands of independent square objects, but that's rarely the truth under the hood. Usually they'll combine adjacent colliders into larger contiguous rectangles or an edge collider chain tracing just the outermost perimeter. \$\endgroup\$ – DMGregory Jan 6 at 14:26
  • \$\begingroup\$ But "is X more demanding than Y?" is generally a question you can answer for yourself much faster and with higher confidence than waiting for Internet hearsay on the matter. Just build a test scene with gobs of box colliders and profile it. Then try a scene with gobs of polygon colliders. The results will tell you not only which is more efficient in the abstract, but which is more efficient for your specific use case on your target hardware. Much more directly applicable than advice you'll find online. \$\endgroup\$ – DMGregory Jan 6 at 14:28
  • \$\begingroup\$ @DMGregory Hm, combining them all into a single one sounds good. But I'm not sure the answer you linked will be applicable if I have blocks which are not all aligned (I have blocks going up and down to create more diverse paths in the level), except if I used an Edge Collider. But in my experience relying on collisions doesn't work well for stuff like "seeing if the player is grounded", as it's not really consistent whether the collision actually keeps taking place when he stays on a certain block. \$\endgroup\$ – Eärendil Baggins Jan 7 at 15:13
  • \$\begingroup\$ That's an entirely separate concern, usually solved by some form of raycasting, which works no matter what kind of collision representation you use. \$\endgroup\$ – DMGregory Jan 7 at 15:15
  • \$\begingroup\$ Hm, actually the Unity Standard Assets Project says it uses the overlapCircleAll method just because it doesn't want to modify the layers (as they're meant to be assets to be reused on other projects with different settings), so I'm not sure that's the way... especially if there are many items, that method is gonna do a lot of checks and be really expensive, isn't it? \$\endgroup\$ – Eärendil Baggins Jan 7 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.