Coordinated Movement

Intent

The intent of this project was to create a coordinated movement system that moved units in organized groups in a way that could be used in a real time strategy game.

Features

  • Coordinated movement of individual units in organized groups
  • Support for any formation that has a limited number of members
  • Wheeling of the formations
  • Individual and formation speed adjustment to keep the group cohesive
  • Smooth transitions between formations
  • Natural movement around obstacles
  • Variation in movement and position of units to create realistic formation appearance

Tools

Gallery

Implementation

The system was created in Unity using C# as a scripting language. It is highly extensible and easy to customize and tune depending on the intended use.

The system uses a Virtual Leader to direct the group and a number of positions specified in a formation class to define the shape. Individual unit positions and general movement within the environment are determined using Unity's navmesh components. Virtual Leader and individual unit movement is modulated to keep the group together and in the specified formation.

Formations

The first thing I did to create this system was to put together a basic formation movement system which used one of its members as the leader. This allowed me to understand Unity's navigation mesh components and defined what each formation required.

Each formation is a C# class built off of an abstract base class which tracks an enumerated value defining the formation type, the maximum number of units in the formation, the positions for each of these units, and the center of mass of the formation.

The first thing I did to create this system was to put together a basic formation movement system which used one of its members as the leader. This allowed me to understand Unity's navigation mesh components and defined what each formation required.

Virtual Leader

I then added in a Virtual Leader which is essentially an invisible unit that is used as the origin point for the formation. It keeps track of the formation currently in use, formations available to the group, and the members of the group. As it moves the positions for individual units in the formation change causing the member units to move.

Using a Virtual Leader has two huge advantages when controlling groups of units. First, it allows for easy manipulation of the group's speed as a whole allowing the group to stay together. Second, because the virtual leader is a sort of unit and so has an orientation, wheeling the formation to keep the front of it pointing in the right direction becomes integrated with no extra work.

In order to keep the Virtual Leader from getting too far ahead of the members its speed is modulated by the function below.

Lines 159-164 determine the current center of mass for the group in world space.

Line 166 determines how far the Virtual Leader is from the formation's actual center of mass.

Line 168 sets a speed modifier to modulate the Virtual Leader's speed based on how close it is to a maximum drift distance specified in the editor.

Movement

Finally, I added in a path projection feature to keep the units in constant motion. In order to get their next target position the units contact their Virtual Leader using the function below.

Line 80 gets the unit's position in the formation relative to the origin.

Line 88 transforms that relative position into global space using the Virtual Leader's position as the local origin.

Lines 91-93 use the unit's speed to project ahead on its current path and get an estimate of where the unit will soon be.

Line 94 calculates a new target position by adding the predicted position in the Virtual Leader's local space to the unit's position within the formation.

Because each unit tracks its own target position and speed individually this allowed me to modulate the unit's speed in a way similar to the Virtual Leader's.

In this case the modifier for the speed is calculated in different ways depending on if the unit is ahead of their position in the formation (Lines 58-60) or behind it (Lines 62-64) to allow the unit to speed up and slow down at different rates.

Results

Ultimately the system I created allowed for the units to be moved in groups while avoiding obstacles and staying in their formation. It is easy to extend and customize the system for use in a variety of games and I am pleased with how the movement looks.

Sources

The following sources were used while doing research for this system:

Jurney, Chris. "Company of Heroes Squad Formations Explained." AI Game Programming Wisdom 4. Ed. Steve Rabin. Boston, Massachusetts: Charles River Media, 2008. 61-69. Web.

Millington, Ian, and John Funge. Artificial Intelligence for Games. 2nd ed. Boca Raton, FL: Taylor & Francis Group LLC, 2009. Print.

Pottinger, Dave. "Implementing Coordinated Movement." Gamasutra. N.p., 29 Jan. 1999. Web. 18 Nov. 2015. http://www.gamasutra.com/view/feature/131721/implementing_coordinated_movement.php