A* Pathfinding Project
4.2.7
The A* Pathfinding Project for Unity 3D
|
Tutorial for writing a simple custom movement script.
This tutorial is part of the get started tutorial: Get Started With The A* Pathfinding Project.
We are going to write our own, really simple script for moving the AI, so open your favourite script-editor and follow.
The first thing we need to do is to calculate a path. For this we use the StartPath method on the Seeker component. The call to the Seeker is really simple, three arguments, a start position, an end position and a callback function (must be in the form "void SomeFunction (Path p)"):
So let's start our script with a simple snippet that starts a path request at startup:
Save it to a file in your project named AstarAI.cs and add the script to the AI GameObject.
Create a new GameObject that we can use as a target, move it to the coordinates say (-20,0,22) and then select the AI GameObject and drag the target to the targetPosition field. This is the position the AI will try to find a path to now.
Press Play. You should get the log message and also the path should show up in the scene view as a green line (the Seeker component draws the last calculated path using Gizmos).
If you do not see a green line, make sure that the checkbox Show Gizmos on the Seeker component is checked. More recent Unity versions also depth-test gizmos, so it might be hidden under the ground, to disable the depth-testing click the Gizmos button above the scene view window and uncheck the '3D Icons' checkbox.
In case you get an error, make sure that the Seeker component really is attached to the same GameObject as the AstarAI script. If you still get an error, the target position might not be reachable, try to change it a bit.
It doesn't look very smooth, but that will do for now as you might be waiting for an explanation of what that code really did.
What happens is that first the script calls the Seeker's StartPath method. The seeker will then create a new ABPath instance and then send it to the AstarPath script (the Pathfinder component you added before). The AstarPath script will put the path in a queue. As soon as possible, the script will then process the path by searching the grid, node by node until the end node is found.
For more information about how the searching step works you can take a look at this wikipedia page.
Once calculated, the path is returned to the Seeker which will post process it if any modifiers are attached and then the Seeker will call the callback function specified in the call. The callback is also sent to Seeker.pathCallback which you can register to if you don't want to specify a callback every time you call StartPath:
When we get the calculatated path back, how can we get info from it?
A Path instance contains two lists related to that. Path.vectorPath is a Vector3 list which holds the path, this list will be modified if any smoothing is used, it is the recommended way to get a path. secondly there is the Path.path list which is a list of GraphNode elements, it holds all the nodes the path visisted which can be useful to get additonal info on the traversed path.
First though, you should always check path.error, if that is true, the path has failed for some reason. The field Path.errorLog will have more info about what went wrong in case path.error is true.
To expand our AI script, let's add some movement.
3D | 2D |
---|---|
We will move the agent using the Unity built-in component CharacterController. So attach a CharacterController to the AI GameObject. | We will move the agent by simply modifying the position of the Transform component. |
The script will keep track of the waypoint in the path that it is moving towards and then change that to the next one as soon as it gets close to one.
Every frame we will do a few things:
If you press play now, the AI will follow the calculated path, neat, eh?
If you want you can attach the SimpleSmoothModifier to the GameObject to get a smoother path. You can read more about modifiers in Using Modifiers.
Here is a video of the movement script in action in a 2D scene with a simple smooth modifier to smooth out the path:
You can find a few additional improvements such as recalculating the path regularly here: AstarAI.cs.
This is the end of this tutorial. I recommend that you continue with the rest of the get started tutorial: Back to the get started tutorial.