I have enemy with simple behavior: Reach the player's current position. In game are increasing waves, which are increasing the number of enemies. But when are more enemies (like 14), and they got too close to each other, there is big physics spike in profiler.


There are significant drop of performance... And as Profiler says, it's because of Physics...

Profiler Profiler2

Now I am little lost, I tried turn of the ignore collision on layer (which was turned on), so they don't overlap, but there is still little touch of course, when they group up, so still so much to process...

What is the best solution here?

I build it around rigidbodies for the use of Velocity, and detection of collisions with walls (edge collider), so when the object reaches there, it is easily stopped... But for this cases it seems now like not the best solution.

  • \$\begingroup\$ What does the profiler say? Also, are you sure you are not leaking destroyed/dead enemies? \$\endgroup\$ – wondra Sep 18 '20 at 13:04
  • \$\begingroup\$ @wondra Everything is clearing, I even have option to start at whatever wave I demand, and there is nothing forgotten. I will try the profiler. \$\endgroup\$ – Zechy Sep 18 '20 at 13:07
  • 2
    \$\begingroup\$ Can you show us 1) How you've configured the rigidbodies and colliders on these enemies, 2) How you're moving them? It looks like you might be getting them into a nasty inter-penetration case that the physics engine spends lots of time trying to solve each frame, before your gameplay code undoes its work and smashes them back together anyway, and there are quite a few things we can do to lessen or eliminate that problem, depending on the details of the causes. \$\endgroup\$ – DMGregory Sep 18 '20 at 16:01
  • 1
    \$\begingroup\$ That info should go in your question. Is it necessary to use a polygoncollider here instead of a more efficient circle or box? In your screenshot it looks like your enemies can overlap. Are they on a physics layer that ignores self-collisions to ensure you're not wasting cycles handling these overlaps? \$\endgroup\$ – DMGregory Sep 18 '20 at 17:50
  • 1
    \$\begingroup\$ Well... I must laugh now... I have tried the circle collider... That solves everything... The polygon collider is of course very complex (generated automaticaly), so that's probably it. I create simplier one. See what I get and post answer here. \$\endgroup\$ – Zechy Sep 18 '20 at 17:59

Ok, as was stated in comments, I tried to use simple circle collider... And now was everything working. The original polygon collider was generated automaticaly by Unity, which was trying cover whole shape of sprite, see below. There was two array of points (one with 22 point, another with 120 points).

Original Collider

As I would like to cover the shape of sprite with the collider, I set up custom, much simplier with 4 points. And is working like with the Circle collider. From 6 thousand contacts to compute, there is only around 30 with the 15 enemies stacked in group.

Simplified Collider

Beware of complex shapes!

  • 1
    \$\begingroup\$ Nice simplification! \$\endgroup\$ – DMGregory Sep 18 '20 at 18:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.