This AI is the default movement script which comes with the A* Pathfinding Project. It is in no way required by the rest of the system, so feel free to write your own. But I hope this script will make it easier to set up movement for the characters in your game. This script works well for many types of units, but if you need the highest performance (for example if you are moving hundreds of characters) you may want to customize this script or write a custom movement script to be able to optimize it specifically for your game.
Here is a video of this script being used move an agent around (technically it uses the Pathfinding.Examples.MineBotAI script that inherits from this one but adds a bit of animation support for the example scenes):
Quick overview of the variables
In the inspector in Unity, you will see a bunch of variables. You can view detailed information further down, but here's a quick overview.
The repathRate determines how often it will search for new paths, if you have fast moving targets, you might want to set it to a lower value. The destination field is where the AI will try to move, it can be a point on the ground where the player has clicked in an RTS for example. Or it can be the player object in a zombie game. The maxSpeed is self-explanatory, as is rotationSpeed. however slowdownDistance might require some explanation: It is the approximate distance from the target where the AI will start to slow down. Setting it to a large value will make the AI slow down very gradually. pickNextWaypointDist determines the distance to the point the AI will move to (see image below).
This script has many movement fallbacks. If it finds an RVOController attached to the same GameObject as this component, it will use that. If it finds a character controller it will also use that. If it finds a rigidbody it will use that. Lastly it will fall back to simply modifying Transform.position which is guaranteed to always work and is also the most performant option.
How it works
In this section I'm going to go over how this script is structured and how information flows. This is useful if you want to make changes to this script or if you just want to understand how it works a bit more deeply. However you do not need to read this section if you are just going to use the script as-is.
This script inherits from the AIBase class. The movement happens either in Unity's standard Update or FixedUpdate method. They are both defined in the AIBase class. Which one is actually used depends on if a rigidbody is used for movement or not. Rigidbody movement has to be done inside the FixedUpdate method while otherwise it is better to do it in Update.
From there a call is made to the MovementUpdate method (which in turn calls MovementUpdateInternal). This method contains the main bulk of the code and calculates how the AI *wants* to move. However it doesn't do any movement itself. Instead it returns the position and rotation it wants the AI to move to have at the end of the frame. The Update (or FixedUpdate) method then passes these values to the FinalizeMovement method which is responsible for actually moving the character. That method also handles things like making sure the AI doesn't fall through the ground using raycasting.
The AI recalculates its path regularly. This happens in the Update method which checks shouldRecalculatePath and if that returns true it will call SearchPath. The SearchPath method will prepare a path request and send it to the Seeker component which should be attached to the same GameObject as this script. Since this script will when waking up register to the Seeker.pathCallback delegate this script will be notified every time a new path is calculated by the OnPathComplete method being called. It may take one or sometimes multiple frames for the path to be calculated, but finally the OnPathComplete method will be called and the current path that the AI is following will be replaced.
Public Methods
OnTargetReached
()
The end of the path has been reached.
Public
void
OnTargetReached ()
The end of the path has been reached.
If you want custom logic for when the AI has reached it's destination add it here. You can also create a new script which inherits from this one and override the function in that script.
This method will be called again if a new path is calculated as the destination may have changed. So when the agent is close to the destination this method will typically be called every repathRate seconds.
This will trigger a path recalculation (if clearPath is true, which is the default) so if you want to teleport the agent and change its destination it is recommended that you set the destination before calling this method.
Draws detailed gizmos constantly in the scene view instead of only when the agent is selected and settings are being modified.
Public
bool
alwaysDrawGizmos
Draws detailed gizmos constantly in the scene view instead of only when the agent is selected and settings are being modified.
constrainInsideGraph
Ensure that the character is always on the traversable surface of the navmesh.
Public
bool
constrainInsideGraph = false
Ensure that the character is always on the traversable surface of the navmesh.
When this option is enabled a GetNearest query will be done every frame to find the closest node that the agent can walk on and if the agent is not inside that node, then the agent will be moved to it.
This is especially useful together with local avoidance in order to avoid agents pushing each other into walls.
This option also integrates with local avoidance so that if the agent is say forced into a wall by other agents the local avoidance system will be informed about that wall and can take that into account.
Enabling this has some performance impact depending on the graph type (pretty fast for grid graphs, slightly slower for navmesh/recast graphs). If you are using a navmesh/recast graph you may want to switch to the RichAI movement script which is specifically written for navmesh/recast graphs and does this kind of clamping out of the box. In many cases it can also follow the path more smoothly around sharp bends in the path.
It is not recommended that you use this option together with the funnel modifier on grid graphs because the funnel modifier will make the path go very close to the border of the graph and this script has a tendency to try to cut corners a bit. This may cause it to try to go slightly outside the traversable surface near corners and that will look bad if this option is enabled.
Warning
This option makes no sense to use on point graphs because point graphs do not have a surface. Enabling this option when using a point graph will lead to the agent being snapped to the closest node every frame which is likely not what you want.
Below you can see an image where several agents using local avoidance were ordered to go to the same point in a corner. When not constraining the agents to the graph they are easily pushed inside obstacles.
endReachedDistance
Distance to the end point to consider the end of path to be reached.
Public
float
endReachedDistance = 0.2F
Distance to the end point to consider the end of path to be reached.
True if this agent currently has a path that it follows.
Public
bool
hasPath
True if this agent currently has a path that it follows.
maxAcceleration
How quickly the agent accelerates.
Public
float
maxAcceleration = -2.5f
How quickly the agent accelerates.
Positive values represent an acceleration in world units per second squared. Negative values are interpreted as an inverse time of how long it should take for the agent to reach its max speed. For example if it should take roughly 0.4 seconds for the agent to reach its max speed then this field should be set to -1/0.4 = -2.5. For a negative value the final acceleration will be: -acceleration*maxSpeed. This behaviour exists mostly for compatibility reasons.
In the Unity inspector there are two modes: Default and Custom. In the Default mode this field is set to -2.5 which means that it takes about 0.4 seconds for the agent to reach its top speed. In the Custom mode you can set the acceleration to any positive value.
pathPending
True if a path is currently being calculated.
Public
bool
pathPending
True if a path is currently being calculated.
pickNextWaypointDist
How far the AI looks ahead along the path to determine the point it moves to.
Public
float
pickNextWaypointDist = 2
How far the AI looks ahead along the path to determine the point it moves to.
In world units. If you enable the alwaysDrawGizmos toggle this value will be visualized in the scene view as a blue circle around the agent.
Here are a few example videos showing some typical outcomes with good values as well as how it looks when this value is too low and too high.
{aipath/picknext/video_02_default.mp4}
Too low A too low value and a too low acceleration will result in the agent overshooting a lot and not managing to follow the path well.
{aipath/picknext/video_02_100.mp4}
Ok A low value but a high acceleration works decently to make the AI follow the path more closely. Note that the AILerp component is better suited if you want the agent to follow the path without any deviations.
{aipath/picknext/video_07_default.mp4}
Ok A reasonable value in this example.
{aipath/picknext/video_10_default.mp4}
Ok A reasonable value in this example, but the path is followed slightly more loosely than in the previous video.
{aipath/picknext/video_20_default.mp4}
Too high A too high value will make the agent follow the path too loosely and may cause it to try to move through obstacles.
reachedEndOfPath
True if the agent has reached the end of the current path.
Public
bool
reachedEndOfPath
True if the agent has reached the end of the current path.
This is the approximate distance the AI has to move to reach the end of the path it is currently traversing.
Note that setting the destination does not immediately update the path, nor is there any guarantee that the AI will actually be able to reach the destination that you set. The AI will try to get as close as possible.
It is very hard to provide a method for detecting if the AI has reached the destination that works across all different games because the destination may not even lie on the navmesh and how that is handled differs from game to game (see also the code snippet in the docs for destination).
Remaining distance along the current path to the end of the path.
Public
float
remainingDistance
Remaining distance along the current path to the end of the path.
For the RichAI movement script this may not always be precisely known, especially when far away from the destination. In those cases an approximate distance will be returned.
If the agent does not currently have a path, then positive infinity will be returned.
Rotation is calculated using Quaternion.RotateTowards. This variable represents the rotation speed in degrees per second. The higher it is, the faster the character will be able to rotate.
slowdownDistance
Distance from the end of the path where the AI will start to slow down.
Public
float
slowdownDistance = 0.6F
Distance from the end of the path where the AI will start to slow down.
slowWhenNotFacingTarget
Slow down when not facing the target direction.
Public
bool
slowWhenNotFacingTarget = true
Slow down when not facing the target direction.
Incurs at a small performance overhead.
steeringTarget
Point on the path which the agent is currently moving towards.
The character can either stop immediately when it comes within that distance, which is useful for e.g archers or other ranged units that want to fire on a target. Or the character can continue to try to reach the exact destination point and come to a full stop there. This is useful if you want the character to reach the exact point that you specified.
Note
reachedEndOfPath will become true when the character is within endReachedDistance units from the destination regardless of what this field is set to.
Inherited Public Members
canMove
Enables or disables movement completely.
Public
bool
canMove = true
Enables or disables movement completely.
If you want the agent to stand still, but still react to local avoidance and use gravity: use isStopped instead.
This is also useful if you want to have full control over when the movement calculations run. Take a look at MovementUpdate
Offset along the Y coordinate for the ground raycast start position.
Public
float
centerOffset = 1
Offset along the Y coordinate for the ground raycast start position.
Normally the pivot of the character is at the character's feet, but you usually want to fire the raycast from the character's center, so this value should be half of the character's height.
A green gizmo line will be drawn upwards from the pivot point of the character to indicate where the raycast will start.
Position in the world that this agent should move to.
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 should check both #pathPending and #reachedEndOfPath: 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 used for pathfinding as the character's pivot point is sometimes placed at the center of the character instead of near the feet. In a building with multiple floors the center of a character may in some scenarios be closer to the navmesh on the floor above than to the floor below which could cause an incorrect path to be calculated. To solve this the start point of the requested paths is always at the base of the character.
gravity
Gravity to use.
Public
Vector3gravity = new Vector3(float.NaN, float.NaN, float.NaN)
Gravity to use.
If set to (NaN,NaN,NaN) then Physics.Gravity (configured in the Unity project settings) will be used. If set to (0,0,0) then no gravity will be used and no raycast to check for ground penetration will be performed.
If this is set to true the agent will immediately start to slow down as quickly as it can to come to a full stop. The agent will still react to local avoidance and gravity (if applicable), but it will not try to move in any particular direction.
The current path of the agent will not be cleared, so when this is set to false again the agent will continue moving along the previous path.
This is a purely user-controlled parameter, so for example it is not set automatically when the agent stops moving because it has reached the target. Use #reachedEndOfPath for that.
If this property is set to true while the agent is traversing an off-mesh link (RichAI script only), then the agent will continue traversing the link and stop once it has completed it.
Note
This is not the same as the canMove setting which some movement scripts have. The canMove setting disables movement calculations completely (which among other things makes it not be affected by local avoidance or gravity). For the AILerp movement script which doesn't use gravity or local avoidance anyway changing this property is very similar to changing canMove.
The #steeringTarget property will continue to indicate the point which the agent would move towards if it would not be stopped.
Direction and distance to move the agent in world space.
)
Move the agent.
This is intended for external movement forces such as those applied by wind, conveyor belts, knockbacks etc.
Some movement scripts may ignore this completely (notably the AILerp script) if it does not have any concept of being moved externally.
The agent will not be moved immediately when calling this method. Instead this offset will be stored and then applied the next time the agent runs its movement calculations (which is usually later this frame or the next frame). If you want to move the agent immediately then call: ai.Move(someVector); ai.FinalizeMovement(ai.position, ai.rotation);
the rotation that the agent wants to rotate to during this frame.
)
Calculate how the character wants to move during this frame.
Note that this does not actually move the character. You need to call FinalizeMovement for that. This is called automatically unless canMove is false.
To handle movement yourself you can disable canMove and call this method manually. This code will replicate the normal behavior of the component: void Update () { // Disable the AIs own movement code ai.canMove = false; Vector3 nextPosition; Quaternion nextRotation; // Calculate how the AI wants to move ai.MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation); // Modify nextPosition and nextRotation in any way you wish // Actually move the AI ai.FinalizeMovement(nextPosition, nextRotation); }
onSearchPath
Called when the agent recalculates its path.
Public
System.Action
onSearchPath
Called when the agent recalculates its path.
This is called both for automatic path recalculations (see canSearch) and manual ones (see SearchPath).
If updateRotation is true then this value is identical to transform.rotation.
rotationIn2D
If true, the forward axis of the character will be along the Y axis instead of the Z axis.
Public
bool
rotationIn2D = false
If true, the forward axis of the character will be along the Y axis instead of the Z axis.
For 3D games you most likely want to leave this the default value which is false. For 2D games you most likely want to change this to true as in 2D games you usually want the Y axis to be the forwards direction of the character.
SearchPath
()
Recalculate the current path.
Public
void
SearchPath ()
Recalculate the current path.
You can for example use this if you want very quick reaction times when you have changed the destination so that the agent does not have to wait until the next automatic path recalculation (see canSearch).
If there is an ongoing path calculation, it will be canceled, so make sure you leave time for the paths to get calculated before calling this function again. A canceled path will show up in the log with the message "Canceled by script" (see #Seeker.CancelCurrentPathRequest()).
If no destination has been set yet then nothing will be done.
Note
The path result may not become available until after a few frames. During the calculation time the #pathPending property will return true.
In case the path has not been calculated, the script will call seeker.StartPath to calculate it. This means the AI may not actually start to follow the path until in a few frames when the path has been calculated. The #pathPending field will as usual return true while the path is being calculated.
In case the path has already been calculated it will immediately replace the current path the AI is following. This is useful if you want to replace how the AI calculates its paths. Note that if you calculate the path using seeker.StartPath then this script will already pick it up because it is listening for all paths that the Seeker finishes calculating. In that case you do not need to call this function.
You can disable the automatic path recalculation by setting the canSearch field to false.
// Disable the automatic path recalculation ai.canSearch = false; var pointToAvoid = enemy.position; // Make the AI flee from the enemy. // The path will be about 20 world units long (the default cost of moving 1 world unit is 1000). var path = FleePath.Construct(ai.position, pointToAvoid, 1000 * 20); ai.SetPath(path);
SimulateRotationTowards
(direction, maxDegrees)
Simulates rotating the agent towards the specified direction and returns the new rotation.
Simulates rotating the agent towards the specified direction and returns the new rotation.
Note that this only calculates a new rotation, it does not change the actual rotation of the agent. Useful when you are handling movement externally using FinalizeMovement but you want to use the built-in rotation code.
Determines if the character's position should be coupled to the Transform's position.
Public
bool
updatePosition = true
Determines if the character's position should be coupled to the Transform's position.
If false then all movement calculations will happen as usual, but the object that this component is attached to will not move instead only the position property will change.
This is useful if you want to control the movement of the character using some other means such as for example root motion but still want the AI to move freely.
Determines if the character's rotation should be coupled to the Transform's rotation.
Public
bool
updateRotation = true
Determines if the character's rotation should be coupled to the Transform's rotation.
If false then all movement calculations will happen as usual, but the object that this component is attached to will not rotate instead only the rotation property will change.
Outputs the start point and end point of the next automatic path request.
This is a separate method to make it easy for subclasses to swap out the endpoints of path requests. For example the #LocalSpaceRichAI script which requires the endpoints to be transformed to graph space first.
CancelCurrentPathRequest
()
Protected
void
CancelCurrentPathRequest ()
canMove
Enables or disables movement completely.
Private
bool IAstarAI.
canMove
Enables or disables movement completely.
If you want the agent to stand still, but still react to local avoidance and use gravity: use isStopped instead.
This is also useful if you want to have full control over when the movement calculations run. Take a look at MovementUpdate
A path is first requested by #UpdatePath, it is then calculated, probably in the same or the next frame. Finally it is returned to the seeker which forwards it to this function.