Movement scripts
Brief overview of the different movement scripts in the package.
There are several movement scripts included in the package. These are the scripts that actually move some object in your scene along a path, usually a character of some sort. The movement scripts primary role is to take care of searching for paths and following them.
These scripts are completely optional in this package, you can use pathfinding without using a movement script, or you can write your own movement script. However for many games they provide a nice base to build your characters on, even if you perhaps later in development replace them with something specifically tailored for your game.
There are three included movement scripts in the package AIPath, RichAI and AILerp. The names of the scripts could in hindsight definitely have been chosen better, but I'm hesitant to change them at this point as a large quantity of support material and forum posts refer to these names.
The primary differences between the movement scripts are:
AIPath
Good all-around movement script which works on all graph types.
Follows paths smoothly and responds to physics.
Works well with local avoidance.
Supports movement in 3D games as well as 2D games.
RichAI
Designed specifically for navmesh/recast graphs and does not work with any other graph types.
Better than the AIPath script at following paths on navmesh based graphs, it can handle getting pushed off its path better and usually follows the path more smoothly.
Has better support for off-mesh links compared to AIPath.
Works well with local avoidance.
Supports movement in 3D games (movement in the XZ plane), but not 2D.
AILerp
Uses linear interpolation to move along the path (which is why 'lerp', which stands for linear interpolation, is in the name). Does not use physics in any way.
Follows the path exactly, without any deviations whatsoever.
Due to the above points it does not make sense to use it with local avoidance, and thus it does not support it.
By far the fastest of the movement scripts, because the movement in itself is much simpler, but keep in mind that if you need any kind of physical realism in the game, you should usually use one of the other movement scripts.
Supports movement in 3D games as well as 2D games.
In short, if you are using a navmesh-based graph: use the RichAI script, otherwise use either AIPath or AILerp depending on what kind of movement style your game needs. Check out the example scenes included in the package to see how the different movement scripts behave.
All movement scripts included in the package implement the Pathfinding.IAstarAI interface. So if you need something that can interact with several different movement scripts, this is a nice interface to use.
For example these are some properties which you may find useful (these properties exist on all movement scripts):
If no destination has been set yet, then (+infinity, +infinity, +infinity) will be returned.
Note that setting this property does not immediately cause the agent to recalculate its path. So it may take some time before the agent starts to move towards this point. Most movement scripts have a repathRate field which indicates how often the agent looks for a new path. You can also call the SearchPath method to immediately start to search for a new path. Paths are calculated asynchronously so when an agent starts to search for path it may take a few frames (usually 1 or 2) until the result is available. During this time the pathPending property will return true.
If you are setting a destination and then want to know when the agent has reached that destination then you could either use reachedDestination (recommended) or check both pathPending and reachedEndOfPath. Check the documentation for the respective fields to learn about their differences.
IEnumerator Start () {
ai.destination = somePoint;
// Start to search for a path to the destination immediately
ai.SearchPath();
// Wait until the agent has reached the destination
while (!ai.reachedDestination) {
yield return null;
}
// The agent has reached the destination now
}
IEnumerator Start () {
ai.destination = somePoint;
// Start to search for a path to the destination immediately
// Note that the result may not become available until after a few frames
// ai.pathPending will be true while the path is being calculated
ai.SearchPath();
// Wait until we know for sure that the agent has calculated a path to the destination we set above
while (ai.pathPending || !ai.reachedEndOfPath) {
yield return null;
}
// The agent has reached the destination now
}
This is a best effort calculation to see if the destination has been reached. For the AIPath/RichAI scripts, this is when the character is within endReachedDistance world units from the destination. For the AILerp script it is when the character is at the destination (±a very small margin).
This value will be updated immediately when the destination is changed (in contrast to reachedEndOfPath), however since path requests are asynchronous it will use an approximation until it sees the real path result. What this property does is to check the distance to the end of the current path, and add to that the distance from the end of the path to the destination (i.e. is assumes it is possible to move in a straight line between the end of the current path to the destination) and then checks if that total distance is less than #endReachedDistance. This property is therefore only a best effort, but it will work well for almost all use cases.
Furthermore it will not report that the destination is reached if the destination is above the head of the character or more than half the height of the character below its feet (so if you have a multilevel building, it is important that you configure the height of the character correctly).
The cases which could be problematic are if an agent is standing next to a very thin wall and the destination suddenly changes to the other side of that thin wall. During the time that it takes for the path to be calculated the agent may see itself as alredy having reached the destination because the destination only moved a very small distance (the wall was thin), even though it may actually be quite a long way around the wall to the other side.
In contrast to reachedEndOfPath, this property is immediately updated when the destination is changed.
IEnumerator Start () {
ai.destination = somePoint;
// Start to search for a path to the destination immediately
ai.SearchPath();
// Wait until the agent has reached the destination
while (!ai.reachedDestination) {
yield return null;
}
// The agent has reached the destination now
}
In world units per second.
Includes gravity and local avoidance if applicable. In world units per second.
If you want your character to follow a specific object you can use the included script AIDestinationSetter. This will behind the scenes simply set the destination property every frame to the position of your chosen target.