- Home /
AI fleeing from multiple enemies
So this part of my script makes my A run from multiple enemies at the same time, and for the most part it works. I mainly run into issues if like 2 enemies come from one end of it, and one enemy comes from the other end, it weighs that the two AI are a bigger threat that the One AI so it just runs into the other AI committing suicide. i want it to find all the AI in range, and find the best escape rought.
private void Flee(List<GameObject> objectsToFlee)
{
Vector3 fleeVector = Vector3.zero;
Vector3 vectorAway = Vector3.zero;
foreach (GameObject obj in objectsToFlee)
{
vectorAway = (transform.position - obj.transform.position);
fleeVector = (fleeVector + vectorAway);
}
fleeVector = fleeVector.normalized;
transform.position = transform.position + (fleeVector * moveSpeed * Time.deltaTime);
}
you can get the Vector3.Distance() of all the enemies from each other, then select the 2 enemies that are farthest apart, the escape path will be the center of that distance. You can find the middle point of the distance and create a vector pointing to it. But this code might work weird if they are not surrounded.. I'm not sure, but you may have to check for that and toggle between your current behaviour and surrounded behaviour. You could probably figure out if your character is surrounded by averaging all of it's enemies positions.
Hey, @Jdogmaster did u figure out this problem, cause I am also looking for a solution to this problem. I am working on a gam similar to agar.io
Your answer
Follow this Question
Related Questions
make an AI Flee 0 Answers
AI Player Jerky Movement - Overcoming Obstacle 0 Answers
Light detecting + Light lowers variable 2 Answers
AI fleeing - 1 Answer
How do I make an Active 3D AI Ragdoll like in this video? 1 Answer