1
\$\begingroup\$

I am creating an AI that moves around to certain points, but should interact with ais or objects seen.

The ai should find other ai to comunicate or look for objects on the ground.

It works with all those solutions with a low number. But when I have +300 ais the fps drops a lot, caused by Physics2D.FixedUpdate or something related with physics.

How would you deal with this?

I have tried those solutions:


Solution 1

AI:

  • Rigidbody2D Kinematic
  • SphereCollider2D Trigger
  • OnTriggerEnter & Exit Script.

Objects

  • Rigidbody2D Kinematic
  • SphereCollider2D Trigger

Solution 2

AI:

  • Rigidbody2D Kinematic
  • SphereCollider2D Trigger
  • OnTriggerStay Script executed each 120 frames & a Coroutine to simulate OnTriggerExit.

Objects

  • Rigidbody2D Kinematic
  • SphereCollider2D Trigger

Solution 3

AI:

  • No RigidBody
  • SphereCollider2D Trigger
  • Cone of Rays executed each 2 second to detect colliders at certain layers.

Objects

  • No RigidBody
  • SphereCollider2D Trigger

EDIT

In solution 1 and 2, all AI have an "eyes" children GameObject with a TriggerCircleCollider2D and no RigidBody2D, to detect near AIs and GroundObjects.

\$\endgroup\$
  • \$\begingroup\$ Definitely do not use solution 3. If you have a collider without a RigidBody, the physics engine thinks of it as static and optimizes its lookups on the assumption that it shouldn't move regularly. If you do move it, it needs to update those acceleration structures. Doing this a lot (every frame for 300+ objects) will bog down performance. \$\endgroup\$ – DMGregory Feb 26 at 12:17
  • 1
    \$\begingroup\$ If you need 300+ AIs you might want to look into Unity's ECS \$\endgroup\$ – ThePumkinMelon Feb 26 at 14:25
  • \$\begingroup\$ My AI is well designed. My real problem is detecting ohter entities or random objects with rigidbody and trigger. My pathfindign supports 900 ais at 60fps with a character controller. ECS sounds interesting @ThePumkinMelon! \$\endgroup\$ – Jordi Huertas Feb 26 at 16:01
0
\$\begingroup\$

The best solution was solution 3.

With the solutions 1 & 2 with RigidBodies, Physics2D Update Profiler was around 80% usage, now it is at 12%. I need to remove more RigidBodies from the scene.

I created a RayCasting & Layer system with Coroutines with the best practices, for example using Arrays instead of Lists.

Now I can have 1000 agents at 50fps.

Related to @ThePumkinMelon recomendation, ECS, those links helped me:

\$\endgroup\$

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.