A* Pathfinding Project  4.0.3
The A* Pathfinding Project for Unity 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Events Macros Groups Pages
Writing Modifiers

Modifiers are small scripts which post-process paths to for example simplify or smooth them.


They are built into the system using extendable add-on architecture which means that it is easy to add your own modifier.
In this tutorial I will show you how to write a simple path smoother similar to the one included in the project (Components–>Pathfinding–>Modifiers–>SimpleSmooth)

New Script

Begin by creating a new C# script somewhere in your project, name it ModifierTutorial.
Open it up in your favourite script editor, you will have a basic class looking something like this:

using UnityEngine;
using System.Collections;
public class ModifierTutorial : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}

Now, we are going to make a modifier which can be attached to any GameObject with a Seeker, to do that we need to inherit from the MonoModifier class.
This class will handle basic communication between the Seeker and the Modifier and will greatly help the writing of the modifier.
It is an abstract class, so some functions need to be implemented in our modifier to not throw compiler errors:

using UnityEngine;
using System.Collections;
using Pathfinding;
[System.Serializable]
public class ModifierTutorial : MonoModifier {
public override int Order { get { return 60; } }
public override void Apply (Path p) {
}
}

What we have here now is the most basic modifier... which doesn't really do anything, but it will serve as a template for future writing of modifiers.
I have added the "using Pathfinding" statement because MonoModifier exists in the Pathfinding namespace, so we need to include it in our script.
There is also the Order property which decides when, in relation to other modifiers, this modifier is going to get called. The higher the value the later it is going to get called. So if you have one modifier with an order of 10 and one with order of 20, the one with an order of 10 will be called first. The built in modifiers use values ranging from 0 to 50, since we have set the order of this modifier to 60, it will be executed after all built in modifiers (if any are attached).

The Apply function is where we are going to put our code, it will be called when a path needs post-processing.
The Path object supplied is the path which we are going to post-process.

Smoothing the Path

The smoothing algorithm we are going to use is quite simple, it should just draw the points closer together by some amount, and we are going to work on the Pathfinding.Path.vectorPath array, the Pathfinding.Path.path array should never be changed as it might break other modifiers.
First though we need to check if the path suceeded and if the vectorPath array is not null, otherwise we might end up with evil NullReferenceExceptions
And also, how are we going to smooth the path if there is less than three elements in it, it can't be done, so we will skip that case too.

public override void Apply (Path p) {
if (p.error || p.vectorPath == null || p.vectorPath.Count < 3) {
return;
}
}

Then for the actual smoothing, the algorithm will work as follows:
subdivide the path into smaller segments, then loop through the path array, and move each point except the first and the last ones closer to it's adjacent points, do that a number of times to get the desired smoothing.

// The number of times to apply smoothing
public int iterations = 5;
// The number of times to subdivide the original path
public int subdivisions = 2;
public override void Apply (Path p, ModifierData source) {
if (p.error || p.vectorPath == null || p.vectorPath.Count < 3) {
return;
}
// Subdivisions should not be less than zero
subdivisions = subdivisions < 0 ? 0 : subdivisions;
// Prevent unknowing users from entering bad values
if (subdivisions > 12) {
Debug.LogWarning ("Subdividing a path more than 12 times is quite a lot, it might cause memory problems and it will certainly slow the game down.\n" +
"When this message is logged, no smoothing will be applied");
subdivisions = 12;
return;
}
// Create a new list to hold the smoothed path
List<Vector3> newPath = new List<Vector3>();
List<Vector3> originalPath = p.vectorPath;
// One segment (line) in the original array will be subdivided to this number of smaller segments
int segLength = (int)Mathf.Pow(2,subdivisions);
float oneDivLength = 1F / segLength;
for (int i=0;i<originalPath.Count-1;i++) {
for (int j=0;j<segLength;j++) {
//Use Vector3.Lerp to place the points at their correct positions along the line
newPath.Add(Vector3.Lerp (originalPath[i],originalPath[i+1],j*oneDivLength));
}
}
// Assign the last point
newPath.Add(originalPath[originalPath.Count-1]);
// Smooth the path [iterations] number of times
for (int it = 0; it < iterations; it++) {
// Loop through all points except the first and the last
for (int i=1;i<newPath.Count-1;i++) {
// Set the new point to the average of the current point and the two adjacent points
Vector3 newpoint = (newPath[i] + newPath[i-1] + newPath[i+1]) / 3F;
newPath[i] = newpoint;
}
}
// Assign the new path to the p.vectorPath field
p.vectorPath = newPath;
}

Note that the new path gets assigned to the p.vectorPath field, that will enable other scripts to find it.

Pooling

For additional memory efficiency, you can pool the lists so that the garbage collector doesn't have to work so hard.

// Get an empty list from the pool
List<Vector3> newPath = Pathfinding.Util.ListPool<Vector3>.Claim();
List<Vector3> originalPath = p.vectorPath;
...
// Assign the new path to the p.vectorPath field
p.vectorPath = newPath;
// Pool the previous path, it is not needed anymore
Pathfinding.Util.ListPool<Vector3>.Release(originalPath);
See Also
Pooling

The End

That was the end of this tutorial, I hope it will help you get started writing path modifiers.