Large numbers of rigidbodies

Hi all,

I’m creating an ant-like simulation involving lots of AI agents.

Without character controllers I can get up to 250 ants moving at once, but they often go where they shouldn’t due to my lame AI usage.

With character controllers the ants never go where they shouldn’t (because they physically can’t) but I can only get about 50 moving at once.

Is there a way for me to give the ants rigidbodies (which makes them easier to handle) and have loads (100+) moving at once? Or is there a limit to the number of rigidbodies you can have moving in a scene at one time?

Thanks in advance, I can elaborate further if this is too vague…

Update:

I’ve found that it is only large amounts of character controllers that slow things down. If I use rigidbodies I can get 100+ agents moving on-screen… but they are much harder to steer!

Well that is because the way character controllers work, they are not directly controlled by the physics engine but rather through a process of recursive linear casting (to ensure they dont tunnel through anything – and if they are bumping into each other will its going to be hell).

Now the rigid bodies are not going to have this problem because they are simply controlled via the physics simulation. They of course will not behave the way you are used to (in games) because that as it turns out is not how real physics works.

Thanks aiursrage2k, that makes sense.

Now that I’m using rigidbodies I can get 200+ agents moving. To make them more manageable I use a high drag value (20) and ForceMode.VelocityChange. This way they behave quite like character controllers.

It’s actually a little better than with character controllers as the agents jostle and bump each other in a more life-life manner.

Basically the things that are slowing my code down now are raycasts and overlapSpheres… With some tinkering I may be able to get even more agents on-screen!

Has anyone had more that 200 AIs on-screen at once? Might be nice to see some examples :slight_smile:

Here’s mine:

http://www.jonmallinson.co.uk/storage/unity/ants/ants.html

Click on the floor to freak out the ant!

I’ve put the agent count down to 100 here but I can get it up to 200 on my laptop… 300 and it starts to implode…

In fact, please be warned, this build is a little unstable - it just crashed my browser…

30 seconds and my browser crashed. interesting though i’d recomend working on a stable build then you can reoptimise for more AI

Worked good for me. Juddered for first few seconds, then very smooth (Chrome). Very cool little toy :slight_smile:

Hi, for anyone interested here is a non-crashy version of my ants:

http://www.jonmallinson.co.uk/storage/unity/antstable/antstable.html

I believe the issue was some while loops that were sometimes getting very long. I’ve tried to remedy it by making the while loops into coroutines with yield returns in each loop. This way the ant ‘thinks’ over a number of updates instead of doing all it’s thinking in a single update.

Performance seems to have actually gone down, but it is at least much more stable… Optimization will continue!