Getting AI to Swarm Around the player

I have a simple ai that chases the player and attacks when within a specific range and it works ok, however there is a problem where if there are many of this ai chasing the player they basically get stuck behind one another in front of the player where basically only like 2 or 3 are attacking and the rest are just stuck behind those 2 or three. Is there a way to make them walk around those 2 or 3 and encircle the player instead of just standing in the front so more of the ai are actually attacking the player

Here Are Some Screenshots from my project:





I don’t know if there is a magic mode in unreal, what I do is create a squad controller that has the npcs in an array to control that only one is attacking.

To make the AI characters move towards the player and perform specific actions when they reach a certain distance, you can design a behavior tree. Use a sequence node to define the actions that the characters should perform. These actions can include spreading out in a circle around the player or flanking them from the sides.

You can utilize the AI Perception system in Unreal Engine to enable the AI characters to perceive and respond to the environment and other characters in the game. By setting up the AI characters to detect the player, you can employ a blend of movement and attack behaviors to make them move around the player and attack from diverse angles.

Creating an AI swarm around the player can be accomplished using various techniques, such as behavior trees, finite state machines, and potential fields. Here is a general approach you could take:

  1. Determine the behavior you want the swarm to exhibit. For example, do you want them to follow the player closely or keep a certain distance? Do you want them to attack the player or just surround them?
  2. Implement a system to detect the player’s position. You can do this by using raycasts, sensors, or simply tracking the player’s position.
  3. Create a swarm entity that consists of multiple individual agents. Each agent should have a set of behaviors that allow it to navigate the environment and interact with other agents in the swarm.
  4. Implement a swarm behavior that will dictate how the agents move in relation to the player. This behavior could be a simple flocking algorithm or a more complex potential field that takes into account the player’s position and movement.
  5. Fine-tune the swarm behavior to achieve the desired effect. You may need to experiment with different parameters such as the swarm’s cohesion, alignment, and separation to get the behavior just right.
  6. Add additional behaviors to the swarm agents as needed, such as attacking the player or dodging obstacles in the environment.
  7. Test and iterate on the swarm behavior until it feels engaging and challenging for the player.

Overall, creating a swarm behavior can be a complex task, but by breaking it down into smaller steps and iterating on your approach, you can create a compelling and dynamic AI experience for players.

You upvoted the ChatGPT answer :frowning:

1 Like

…never mind the fact that it doesn’t even answer the question.

A quick and easy solution is to draw a line trace between an AI and the player when the AI is close enough. If it hits another enemy, give that AI a new spot on (NOT in, on) a circle of specified radius that surrounds the player.

You can use unit circle maths to determine a location relative to the player when given a radius. It’s then just a matter of moving the AI to that point and facing the player. You could add additional checks to determine if the AI overlaps another enemy and if it does then move it several units away, or just employ AI-to-AI collision from the start so they’re never overlapping in the first place.