#Optimizing Many-Unit Interactions

1 messages · Page 1 of 1 (latest)

hoary mason
#

I have a game with many units which all follow a series of behavior patterns. They should change their current behavior or behavior pattern depending on certain conditions, almost all of which have to do with their proximity to certain triggers. For example, a Scout should return to the Colony when it comes within range of whatever it is scouting for (eg, food). An Attendant should increase the progress on hatching an egg when it is close enough to the Queen. What is the best way to manage this system? Currently, I'm using OnCollisionEnter and OnCollisionStay to drive behavior changes, but the physics of it all is pretty unnecessary for my game, and I'm worried all the collision logic is slowing things down. Is detecting GameObjects within a radius cheaper? Is it feasible to have GameObjects inform other GameObjects of their proximity somehow?

#

I'm at the stage of my game where I need to land on a more permanent solution to unit behavior and physics. I don't need faithful physics, esp gravity. I mostly need my units to stay inside the world, avoid clipping through each other too much if possible (not really that important except for polish), and to detect whether they're within range of "food" or "another unit" or "the colony" etc.

fathom carbon
#

well, you detect game objects within a radius by using physics queries!

hoary mason
#

That makes sense.

fathom carbon
#

physics are a very efficient way to detect what things are near you

hoary mason
#

I got up to ~100 units last night and I think performance tanked -.-

fathom carbon
#

you don't have to compare every object's position to every other object's position; there are some optimizations in there

clear parrot
#

You can detect distance with simple Vector math. no physics system needed

hoary mason
fathom carbon
#

i would probably go with overlap queries that only fire every now and then

hoary mason
#

Or else you have to track when units enter and exit a radius or other units?

fathom carbon
#

and have a layer dedicated to things you'd want to detect

hoary mason
fathom carbon
#

yeah

hoary mason
clear parrot
#

It would be more efficient than doing the same with collision detection.

fathom carbon
#

now, I think it's also reasonable to just do some distance calculations if you have a small set of things you're looking for

hoary mason
fathom carbon
#

and you can always profile it to see which one works out better

hoary mason
#

true

#

part of me wonders if i would have been better off making the game 2D from the start. is that more efficient?

#

that's a dumb question, it must be lol

clear parrot
#

The rub is that if you want your units to avoid eachother "Not Clip through eachother" you'll need some sort of path finding, and distance alone wont get you there

fathom carbon
#

triggers are simpler: they don't do any actual physical interactions

hoary mason
hoary mason
clear parrot
hoary mason
hoary mason
#

i want them to behave like ants if you couldn't tell lol

#

(they are ants)

clear parrot
#

I dunno. I've never tried to roll my own pathfinding. Unity has always done the trick

hoary mason
fathom carbon
#

I guess you could use nav mesh agents

#

They stick to the surface they’re on

hoary mason