A* Pathfinding Project
4.1.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.
In the inspector, change 'Target Position' to something like (-20,0,22). 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 latest 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 sent 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.
The searching step is explained really well in this tutorial here. It does not work exactly like in the tutorial, but the concept is the same.
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: Also add the CharacterController to the AI gameObject.
If you press play now, the AI will follow the calculated path, neat, eh?
What the code does is to, in Update get the normalized direction towards the next waypoint and move towards it a bit. Then it checks if it is close enough to continue to the next waypoint, in this example that is done by simply by incrementing the currentWaypoint index. The AI will stop a short distance from the end point, but that's just because we haven't got any special logic for the last point.
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.