Home

Awesome

Unity-Formation-Movement2.0

Formation movement for Unity 3D using built in NavMesh navigation or A*Pathfinding

Summary

This Github repo contains the 2.0 version of my Formation Movement scripts rebuilt from scratch. The Formation Movement 2.0 allows you to move objects in a present formation shape towards a destination. It supports both AStarPathfinding and Unity built-in NavMesh navigation. Other features include:

Version History

Pre-requisites

Quick start guide (Unity NavMesh)

Below script lists the steps explained in this video.

Tutorial

Create an environment for the formation to walk on.

Environment

The Cylinder at the top left will serve as the destination for the formation.

Prepare the Layers for the scene:

Environment

Assign the Terrain Layer to the Plane and the Obstacles layer to the Cubes. Next set the Plane and cubes to static.

Environment

Setup the Navmesh by using the [Bake] button:

Environment

Create a prefab from a sphere.

Environment

Click on the menu Window | Goodgulf | Formation Setup:

Environment

Click on "Set Navigation to Unity Navmesh" and wait for a couple of seconds to ensure compiling of the scripts has finished (typically NavMesh is the default so a compile is not necessary in a fresh project). Next click on "Create a formation":

Environment

The Formation and the Leader object have now been added to the hierarchy:

Environment

Add the Formation Follower script to the Sphere Prefab and set its Follower Stuck Mode to Random Walk:

Environment

Move the Sphere as a child object to the Leader and reset it’s coordinates then move it up a little:

Environment

Disable the Formation Follower script on the Sphere attached to the Leader.

In the Formation make the following changes:

Environment

Environment

Environment

In the Formation Agent script at the Cylinder from the hierarch into the Destination:

Environment

Now run the scene!

Additional Instructions

AStarPathfinding

After importing AStarPathfinding into your project, change the Formation to support AStartPathfinding by opening the Formation Setup (menu Window | Goodgulf | Formation Setup). Select the option "Set Navigation to AStarPathfinding" and wait for the compilation of the code to finish. Use your regular AStarPathfinding workflow to add navigation to your terrain. Don't forget to add any obstacle to the Obstacles Layer. You can use the Formation Setup menu to create a new Formation and Leader using AStarPathfinding or manually add the components. This is what the Formation object can look like:

Environment Environment

The Seeker and AIDestinationSetter scripts are part of the AStarPathfinding solution. AIPathWithEvents inherits from AStarPathfinding's AIPath script; it adds an event to indicate the formation reached its destination.

Movement Animation

To use movement animation make sure you first import the Unity package mentioned in the pre-requisites section. It contains the animations and locomotion controller used by the FormationAnimation script you can add to the prefabs used by the formation leader and followers:

Environment

Environment

A typical setup of the script for the prefabs looks like this:

Environment

Basically the FormationAnimation script calculates the speed of the object and sets the Animation Controller's parameters (velx, vely, move) based on the speed of the object. An animation blend tree mixes the animations (contained in teh Unity download) for the object. This works best for a humanoid character. The Audio Source contains the sound clip which will be played when the object is moving (in this case marching boots).

Documentation

All scripts contain documentation which I think are sufficient to explain how this version of the Formation Movement works.

Pull Requests

As per below statement on support, I'll look at pull requests and see if changes/fixes you propose should be merged. Please note that I'm not a full time Unity developer so I'll need to do this outside working hours and in the weekend. Any request may be delayed.

Videos

Work in Progress 1 Work in Progress 2 Work in Progress 3 Work in Progress 4

Support

The scripts are free to use (see the license), feel free to ask questions or make pull requests but don’t expect on the spot support. The best place to ask questions is to leave a comment in my YouTube Channel. I'd appreciate it if you subscribe to the channel and like the Formation Movement videos if you plan to use this code.