A* Pathfinding Project
4.1.19
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 abstract 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 writing modifiers in the future.
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.
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 it consists of only a single segment (i.e <= 2 points), 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 its adjacent points, do that a few times until the path is sufficiently smooth.
Note that the new path gets assigned to the p.vectorPath field, that will enable other scripts to find it.
For additional memory efficiency, you can pool the lists so that the garbage collector doesn't have to work so hard.
That was the end of this tutorial, I hope it will help you get started writing path modifiers.
You can find the full script here: ModifierTutorial.cs