Hi, I have been trying to solve a problem and having no luck!
I have an enemy class, and I am checking to see if other enemies are within a certain distance between each other, and if they fall below that distance, one of them should stop to let the other go past.
the way I am doing it is to simply circlecast out, get an array of other enemies, do a quick check against their distance from this enemy. The problem is that all of them, if they are below the distance will stop, and I cant figure out how to make one of them authoritive.
So lets say enemy A and enemy B, are both too close to each other. How does one or the other, tell the other one to move on, without them both applying the same logic?
Ugh, I suspect it might be rather easy, but this is the end of a 13 hour coding session, and my brain is fried! Help me out please
If you want the “authoritative” enemy to be random, you could use a dead simple approach like this:
{
// Inside your collision detection code...
int myPriority = this.GetInstanceId(); // Unique id which can be used as "priority"
int collidingEnemyPriority = collidingEnemy.GetInstanceId();
if (myPriority < collidingEnemyPriority)
{
// I have lower priority, so I should stop...
// The colliding enemy will run the same piece of logic, but will not stop, because their priority will be higher.
Stop();
}
}
Note that you can use any different sort of id for the priority. But using the instance id is nice because it is guaranteed to be unique.
There are many other ways. For example, each Enemy could keep a list of which other enemies they are colliding with. On collision with another enemy, you must check if you are not already in their list. Based on that, you can run your logic for only one of the enemies.
Typically you would give them priorities. Enemy class would have a float value for priority and when it encounters another it checks its priority against theirs to determine if it procedes or yields. There’s generally no need for one unit to TELL the other unit to yield/procede because the other unit would have made the same check and made the opposite decision.
Oh thanks both for the suggestions… of course it was simple… quite why I have been dreaming up such convulted methods of doing this I have no idea… its late
Thanks again, you’ve saved me from banging my head against the wall tonight