How to keep AI Enemies side by side?

I’m making a Tower Defense game and I want to spawn enemies in pairs side by side. This works fine, but as soon as they get to a corner, the one on the outside will wait for the one on the inside to get in front and then they will follow along and it looks kind of stupid. See here.

If there is an enemy beside him, is there a way to make him keep going forward and only move in once there’s space? I realize he would fall behind, but if there are four pairs of enemies, one line would just be slightly behind the other. And it would even out in the next turn assuming it turns the other way. But I’m not sure of any obvious way to do this.

Anyone have any ideas how to keep an enemy in their lane if there’s an enemy in the next lane while going around a turn? Or even in a straight line. They all aim toward the same point at the next corner and they scrunch up together.

I am by no means an expert but I’ve also needed to make AI that can move in loose formations, or in relation to another, and follow paths.

The general method I’ve used is waypoints.

For example, if I want for an enemy to circle the player, I create waypoints around the player (e.g. start from players location, add a rotation + distance, go up 5 meters and then line trace down and get a navigable spot near the hit, do this as many times as you want while incrementing the rotation).

Then the enemy finds the closest waypoint that can be reached and moves to it.

I’ve used setup like this to make it so that enemies in a third person game always circle the player and don’t bunch up (to ensure that, I mark waypoints as being occupied when an enemy is near them (this requires waypoints to be actual actors, rather than just vectors)).

I believe a similar thing might work for you.

You’d need to designate one AI unit as leader and another as follower. Create a waypoint in relation to the leader, and have that be the goal for the follower to move to.

Naturally there will be caveats you have to handle like what if hte location is out of bounds, what if leader is killed, etc - but the basic idea is just that you use some basic vector math + line trace to find a location that is always relative to the unit you want to follow.

Using some debug spheres and debug strings help a lot during development and testing.

You can also look into the detour ai controller to make the ai’s more aware of their companions.

Also you could pre calculate the path then have the ai’s walk the same path with their respected offsets. There is a node that per-calculates the route and sends it back in the form of a vector array.

That last idea is what I think might work best. Problem is right now, it automatically recalculates the path if something opens up or a tower is built. In a previous project, I used AStar and had a custom PathFollowingComponent. Can I have a custom PathFollowingComponent used with a UCharacterMovementComponent? If so, I could widen the agent radius to make the path follow the center of the tiles and in UPathFollowingComponent::FollowPathSegment(), I could adjust the next path point. Or maybe use SetMoveSegment(). Dunno. I just don’t remember how to hook up the path following component to the character movement component.

Made a quick example that might give you an idea.

Last point rotation would need to look back and do a 180 on the z axis. Sorry got a bit lazy :stuck_out_tongue:
It’s missing the +1 point to look at so it goes haywire.

You could also make the other ai just play follow the leader and put in the 1st AI’s location with a radius overlap for stopping. But then they would be single line.

I took a look at your project. I have no idea how you did it that quickly. :slight_smile: The idea I have is similar, but I need to keep it within the nav mesh path generation. Had a look at my old project. I’ll need a custom AI Controller so I can set my own PathFollowingComponent. Inside the PathFollowingComponent, I can dynamically adjust the path at each segment similar to what you’re doing in your blueprint.

Thanks for your help. I’m gonna give this a try.

In my project notice that all points are projected onto the navpath before final plotting, so all should be ok. You can play with the projection extent, just watch the z axis or it can project above the path. (on cliffs etc)

I think I’m close enough now to get what I want. I created a new AIController and was able to specify the PathFollowingComponent that adjusts the path. Unfortunately, they get too close to the corners. But I think I can compensate for that. Also, if the path changes, it doesn’t call RequestMove(). So I’ll have to figure out what it calls to recalculate the path.

Anyhow, we can see here they are indeed following side by side.

Thanks again for your help.

It’s working perfectly now. The corner thing was because it kept losing the Agent Radius in the NavMesh. I had to update it in the project settings in the supported agents and that fixed it.

It looks so good now! :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.