If i select a group of units and send them somewhere i form them like the following;

enter image description here

If i select them and order an attack, i want three things to happen:

  1. High range units should keep the distance while attacking.
  2. Each unit should attack if possible.
  3. Attack formation should not look too unnatural.

Now i can handle the first two by forming the units as a circle by considering the range and unit count etc. But it looks very unnatural.

enter image description here

It's acceptable if some of the units are being idle for some reason, but i want most of the units join attack and they don't look so artificial.

Any ideas on that? Thanks.

  • \$\begingroup\$ Presumably you've searched previous Q&A about formations for some ideas? What have you tried based on your research thus far? \$\endgroup\$
    – DMGregory
    Dec 23, 2018 at 17:57
  • \$\begingroup\$ Yes i've seen them but they're mostly about unit formation and moving around the map. My question is not exactly about the formation but i couln't find a better word. The question is, where should each unit stay while attacking to enemy in order to utilize maximum number of units. \$\endgroup\$
    – Dodo
    Dec 23, 2018 at 18:20
  • \$\begingroup\$ What are the constraints on this maximization problem? Can you give us an A/B example of what formation you'd consider more "natural"/desirable than what you have? \$\endgroup\$
    – DMGregory
    Dec 23, 2018 at 18:22
  • \$\begingroup\$ That's what i'm trying to find out. if i know what formation would look natural and utilize good number of units i would implement it. Making a circle around enemy is a bad idea obviously, so maybe making arches, like a wireless sign would be better. Smaller number of units gets closer to enemy and larger number of units are behind, some of them are too far cannot even fire. i.imgur.com/pU5jFps.png \$\endgroup\$
    – Dodo
    Dec 23, 2018 at 18:34
  • \$\begingroup\$ I forgot which game it was (Empire Earth probably), but it allowed you to select a formation to be built with the next move command. It hat everything from circles, to blocks to "form one straight line", thereby giving the player all the options for what the troops should do \$\endgroup\$
    – Hobbamok
    Feb 22, 2019 at 8:35

3 Answers 3


A possible approach could be to consider a discrete number of positions on the map.

Then rate each spot on desirability to be at that spot for a certain unit type.

  • Crowded spots get rated down.
  • Sparse spots get rated up.
  • Spots at a good attack distance get rated up.
  • Spots too far from, or for archers, too near to, enemies get rated down.

Then let good spots attract units, and bad spots repel units.

With a little luck, they'll spread out appropriately without looking unnatural.

You would have to account for the problem of getting stuck in 'local optima' though.

  • \$\begingroup\$ I have 32x32 tiles and i know at each frame which tiles have which units, so it's feasible in that respect. If i send a troop to attack an enemy the optimal positions would be around the enemy with the radius of our range, so it would make a circle again wouldn't it? This method would work better if we have different types of units in the war field, some units would stay away from enemy archers but also try to find best position to attack etc. \$\endgroup\$
    – Dodo
    Dec 23, 2018 at 18:44
  • \$\begingroup\$ But it's a good idea to rate positions in general, it's very flexible, for instance i can give more points to tiles that are closer to my movement line, it would prevent units to make a circle. Also in reality there will be more enemy units and probably different kind of units as well, so i will fiddle with the idea and maybe implement it. \$\endgroup\$
    – Dodo
    Dec 23, 2018 at 18:48

Some kind of rudimentary rules based swarming system might work. Basically, give each individual in a unit a set of rules, and treat them as individuals, not as a group. The rules though create group/formation behaviors.

For example imagine you wanted a formation of archers to stick together as best as possible, while maintaining range. And an archer can shoot a distance of 20 (of whatever unit you have).

Start with some basic rules like this...

  1. Archers should try to maximize their range from target (as close to 20 away, but not beyond)
  2. Archers should stay at least 10 units away from the target
  3. Archers should not have more than 2 archers between themselves and the target (to avoid overly deep formations).
  4. Archers should have at least 3 archers with a distance of <= 1 (to encourage grouping)
  5. If given an order to set up a formation at a given location, archers should set up within 5 units of the destination.

Given an order to set up formation at a certain location, the group of archers will all begin to move to the new location, evaluating their movement (or decision to not move) each time step.

  1. Initially, each archer will choose the target deployment spot as their destination.
  2. The first archer who reaches this spot will stop, and "deploy".
  3. Any time an archers movement destination becomes taken, the archer tries to find a suitable destination that meets rules criteria, and sets that new location as their movement destination.
  4. Once set up, all archers continue to check that basic setup rules are in place. If for example the enemy moves too close, archers closest to the enemy will reposition to be farther away.

It would take some tweaking, but if done right you could get some interesting behaviors as a result. Specifically, adding the "no more than 3 archers deep" rule and the "at least 3 archers next to each other" rule will drive some interesting behaviors. I honestly don't know what the result would be of those rules, but it's conceptually a way to start.

You might also have to refine this by having one archer be the leader. Let the leader make initial moves and decisions, and have the others set up based on the leader's decision. The leader might have their own rules for example to pick a new formation location when the formation gets too close to the enemy.

If you search for "rules based flocking model" you'll find a number of references to the concept that go into greater detail.


Right now they're moving until they're at exactly their attack range, and they stand there and shoot. Since they're all the same unity type and all have an equal attack range, they all stand the same distance away.

The simplest way to make them seem more natural is to add something called irregularity. This can be done many ways, but the two that come to mind are: 1) adding irregularity to their attack range or 2) adding irregularity to the distance they travel after coming in range of a target.

  1. Adding irregularity to the attack range is the simplest option. Basically, you can set a variable that will act as a small, random increase or decrease to each unit's attack range. This could be done when you create the unit, or could be done each time an attack order is issued. This way when they move to get in range, they'll all have slightly different ranges and thus won't form a perfect circle. The downside to this technique is that if this is a competitive RTS for example, randomly having more or less attack range could be criticized as unfair if its noticeable.

  2. The second way you could add irregularity is to add momentum to their movement, so instead of stopping on a dime at the exact attack range, they will come to a stop a small, random amount of time after entering attack range. To implement this, you don't necessarily need to add momentum, just add an extra distance for them to move once entering attack range, but if you already have physics then momentum could look and work well.

With either approach, the attackers will not form perfect circles. The idea is to add a small, adjustable irregularity to their actions that you can tweak until it feels right.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .