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?
#Optimizing Many-Unit Interactions
1 messages · Page 1 of 1 (latest)
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.
well, you detect game objects within a radius by using physics queries!
That makes sense.
physics are a very efficient way to detect what things are near you
I got up to ~100 units last night and I think performance tanked -.-
you don't have to compare every object's position to every other object's position; there are some optimizations in there
You can detect distance with simple Vector math. no physics system needed
incl OnCollision?
Yes, but it surely can't be too efficient to be tracking the distance between every unit and every other unit?
i would probably go with overlap queries that only fire every now and then
Or else you have to track when units enter and exit a radius or other units?
and have a layer dedicated to things you'd want to detect
as opposed to every frame?
yeah
Layers are beyond me, but that'll be my next research topic, I suppose
It would be more efficient than doing the same with collision detection.
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
yeah, this feels true to me
and you can always profile it to see which one works out better
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
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
You should definitely be using triggers, rather than colliders
triggers are simpler: they don't do any actual physical interactions
currently the "default" behavior for a unit is to pick a random rotation between -2.5 and 2.5 degrees and move forward by it's "speed". if it detects another unit inside its radius, couldn't it avoid it that way?
Layers and Triggers. I figured I'd have to learn them eventually. lol
In this implementation they could both go the same way, and if they have to resolve this before they keep moving, then they could get stuck. Unity's pathfinding is pretty robust, and easy to use. I'd say have a look at it.
and a little clipping is fine, but I do want them to behave as though they know other units exist
how do you square what is essentially brownian motion with pathfinding? when a unit is too close, pathfind away and then resume brownian motion?
i want them to behave like ants if you couldn't tell lol
(they are ants)
I dunno. I've never tried to roll my own pathfinding. Unity has always done the trick
is there an easy to ditch all colliders but prevent units from falling through the floor? just be careful with their transform.position.y?
and then triggers for proximity?