Flee from Multiple Enemys

I just purchased a*, hoping to use the flee code in my game. I knew it would work for only one point, however I was hoping I could modify it to avoid multiple targets.
Once looking at the code I see that it uses a weighting to take paths that are in the opposite direction to the target which doesn’t make sense for multiple targets.
Has anyone got some pointers on how to achieve what I want?

1 Like

So, the idea I had was to have enemies alter the weights of the pieces of path they’re on, and then lowering them when they leave. Is this possible?


That is possible with some simple modifications to the code.
First, change the GetTraversalCost method on the Path class to be virtual.

Then in the FleePath class, you change it to take an array of Vector3s, i.e the points to flee from. You can then overload the GetTraversalCost method and make it loop through all your fleeing coordinates, check the distance and add some large constant minus that to the cost (coordinates far away should not contribute to a large cost).

1 Like

I’ve now implemented this, and it looks alright sometimes, other times my guys still run towards the enemy. What are reasonable penalties to be adding to the node, and what is a reasonable searchLength to pass in when constructing the path?

EDIT: All good now. the problem with using RandomPath as a base, is that it would randomly pick points next to enemies to move towards. By inheriting from ABPath instead and picking a random end point myself that was sufficiently far from all kown enemies, everything is looking pretty good.

Still, I’m not sure if the weights i’m producing are necessarily “Reasonable” So if you could still give me a heads up on that, it would be great!


The cost the system uses is roughly based on Int3.Precisiondistance. So the cost of moving 10 world units is roughly 101000 = 10000 (since Int3.Precision is set to 1000 by default).

Removed my previous post after reading the code more carefully
But I got another question regarding it - might be a possible bug:

have a look:

Here is what it should look like:

The green vector represents the last line of the code, is it pointing in the intended direction?


Sorry for the late answer.
I do believe it is correct. The aim is not a direction but a point you see. So the final point that aim is set to looks like this:
It will be a point far away in the opposite direction of the point we want to avoid.

1 Like