What is the best way to move as a Unit with A*

Hello, in my game Soldiers move in specified formations, for example a wedge shape. In the first version of my game, I had the leader of the formation use the pathfinding script and manually set the position of troops to an exact distance and spacing from the leader without using pathfinding at all. I did that because I did not know of any way to set a constantly changing destination to keep the spacing and angle of the troops. Hard coding position basically worked but it has some problems with obstacle avoidance, the facing direction of soldiers, and movement speed. For example when a formation turns the soldiers at the end have to move really fast to be at the same angle from the leader.

Since I am remaking my game from scratch and discovered this forum, I was wondering if A* has a few built in features that would help with formations. I am using FollowerEntity now on a grid graph.


This chapter of AI Game Programming Wisdom 4 has some good ideas. I have not implemented these myself for this package, but they might be a good start for you:

2.1 Company of Heroes Squad Formations Explained - AI Game Programming Wisdom 4.pdf (758.7 KB)

Their method of formation movement involves predicting where the leader will be 2 seconds out then making an offset destination for the followers who are also uses A*.

The FollowerEntity does draw a path in advance. Can any of the public methods or variables for FollowerEntity be used to generate a point along the path that the leader has not reached yet?

You can use ai.GetRemainingPath. FollowerEntity - A* Pathfinding Project

I Checked the documentation on that. It seems to take a “buffer” and a stale" as parameters. I don’t understand what either of them are. Can you elaborate please?


They are described in the docs. The buffer is a list that you pass which will be filled with the result. isStale will be true if the path might not be up to date.

I tested the GetRemainingPath and had it print to console and got 8 points generated. I was wondering how the method picks those points. Are they like points that the agent might turn? I noticed the numbers tend to end in nice even integers for both the x and z component, which makes me wonder if those points are actually along the path or just an approximation. I tried to make it draw the lines by copying your documentation example, but my project won’t draw them.

Yes, the points are the remaining corners of the path. They are likely nice round numbers because the navmesh happened to be generated in that way.

Ok, thanks. I also need a direction variable for the lead agent (preferably in Vector3) so that I can set the offsets for the followers in the unit. I tried the rotation variable it returned some kind of “Vector4” usually (0, 0, 0, 1) or (0, 0, 0, -1).