Unit formations


I am looking for a way for my units to stay in formation. At the moment all units want to go to the same location. To prevent all units from competing for the same destination, ive implemented an algorithm that checks whether or not the unit ahead of it has stopped, if so, the unit behind it will stop as well. Though this solves the problem that every unit wants to reach the same spot, all units are arbitrarily positioned when they come to a stop. Currently it looks like this:

I want it to be a little bit more like this:

What would be a good way to set a target for my units, to keep them nicely in a formation?

The easy way is to have a concept of group. The group has a middle position a relative position for each unit (you can compute it before move). When call the pathfinding you use the targetPosition + relative position in group.