A* Pathfinding Project
3.1.4
The A* Pathfinding Project for Unity 3D
|
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)
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:
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 abstarct class, so some functions need to be implemented in our modifier to not throw compiler errors:
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 are also the input and output properties, these tell the Seeker what this modifier accepts and what it outputs. Usually you take a VectorPath as input and output a VectorPath, see Pathfinding::ModifierData for more info.
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 and the source is what the previous modifier outputed, but we don't need to care about it really since it is guaranteed to be ModifierData.VectorPath as we set that as input.
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.
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.
Note that the new path gets assigned to the p.vectorPath field, that will enable other scripts to find it.
That was the end of this tutorial, I hope it will help you get started writing path modifiers.
Add a comment to this page if there is something you do not understand!