Class LegacyAIPath Extends AIPath

Public

AI for following paths.

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 is not written for high performance, so I do not recommend using it for large groups of units.

This script will try to follow a target transform, in regular intervals, the path to that target will be recalculated. It will on FixedUpdate try to move towards the next point in the path. However it will only move in the forward direction, but it will rotate around it's Y-axis to make it reach the target.

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 target variable 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 speed is self-explanatory, so is turningSpeed, however slowdownDistance might require some explanation. It is the approximate distance from the target where the AI will start to slow down. Note that this doesn't only affect the end point of the path but also any intermediate points, so be sure to set forwardLook and pickNextWaypointDist to a higher value than this.
pickNextWaypointDist is simply determines within what range it will switch to target the next waypoint in the path.
forwardLook will try to calculate an interpolated target point on the current segment in the path so that it has a distance of forwardLook from the AI
Below is an image illustrating several variables as well as some internal ones, but which are relevant for understanding how it works. Note that the forwardLook range will not match up exactly with the target point practically, even though that's the goal. This script has many movement fallbacks. If it finds a NavmeshController, it will use that, otherwise it will look for a character controller, then for a rigidbody and if it hasn't been able to find any it will use Transform.Translate which is guaranteed to always work.

Public Variables

closestOnPathCheck

Do a closest point on path check when receiving path callback.

Public
forwardLook

Target point is Interpolated on the current segment in the path so that it has a distance of forwardLook from the AI.

Public

Inherited Public Members

alwaysDrawGizmos

Draws detailed gizmos constantly in the scene view instead of only when the agent is selected and settings are being modified.

Public
autoRepath

Determines how the agent recalculates its path automatically.

Public
CalculateVelocity (position)

Current desired velocity of the agent (excluding physics and local avoidance but it includes gravity).

Public
canMove

Enables or disables movement completely.

Public
canSearch

Enables or disables recalculating the path at regular intervals.

Public
centerOffset

Offset along the Y coordinate for the ground raycast start position.

Public
constrainInsideGraph

Ensure that the character is always on the traversable surface of the navmesh.

Public
desiredVelocity

Velocity that this agent wants to move with.

Public
desiredVelocityWithoutLocalAvoidance

Velocity that this agent wants to move with before taking local avoidance into account.

Public
destination

Position in the world that this agent should move to.

Public
DrawGizmos ()
Public
enableRotation

If true, the AI will rotate to face the movement direction.

Public
endOfPath

End point of path the agent is currently following.

Public
endReachedDistance

Distance to the end point to consider the end of path to be reached.

Public
FinalizeMovement (nextPosition, nextRotation)

Moves the agent to a position.

Public
FindComponents ()

Looks for any attached components like RVOController and CharacterController etc.

Public
GetFeetPosition ()

Position of the base of the character.

Public
GetRemainingPath (buffer, stale)

Fills buffer with the remaining path.

Public
gravity

Gravity to use.

Public
groundMask

Layer mask to use for ground placement.

Public
hasPath

True if this agent currently has a path that it follows.

Public
height

Height of the agent in world units.

Public
isStopped

Gets or sets if the agent should stop moving.

Public
maxAcceleration

How quickly the agent accelerates.

Public
maxSpeed

Max speed in world units per second.

Public
Move (deltaPosition)

Move the agent.

Public
movementPlane

Plane which this agent is moving in.

Public
MovementUpdate (deltaTime, nextPosition, nextRotation)

Calculate how the character wants to move during this frame.

Public
onSearchPath

Called when the agent recalculates its path.

Public
OnTargetReached ()

The end of the path has been reached.

Public
orientation

Determines which direction the agent moves in.

Public
pathPending

True if a path is currently being calculated.

Public
pickNextWaypointDist

How far the AI looks ahead along the path to determine the point it moves to.

Public
position

Position of the agent.

Public
preventMovingBackwards

Prevent the velocity from being too far away from the forward direction of the character.

Public
radius

Radius of the agent in world units.

Public
reachedDestination

True if the ai has reached the destination.

Public
reachedEndOfPath

True if the agent has reached the end of the current path.

Public
remainingDistance

Approximate remaining distance along the current path to the end of the path.

Public
repathRate

Determines how often the agent will search for new paths (in seconds).

Public
rotation

Rotation of the agent.

Public
rotationIn2D

If true, the forward axis of the character will be along the Y axis instead of the Z axis.

Public
rotationSpeed

Rotation speed in degrees per second.

Public
rvoDensityBehavior

Controls if the agent slows down to a stop if the area around the destination is crowded.

Public
SearchPath ()

Recalculate the current path.

Public
SetPath (path)

Make the AI follow the specified path.

Public
ShapeGizmoColor
Public Static Readonly
SimulateRotationTowards (direction, maxDegrees)

Simulates rotating the agent towards the specified direction and returns the new rotation.

Public
slowdownDistance

Distance from the end of the path where the AI will start to slow down.

Public
slowWhenNotFacingTarget

Slow down when not facing the target direction.

Public
speed

Maximum speed in world units per second.

Public
steeringTarget

Point on the path which the agent is currently moving towards.

Public
target

Target to move towards.

Public
targetDirection

Direction that the agent wants to move in (excluding physics and local avoidance).

Public
TargetReached

True if the end of the path has been reached.

Public
Teleport (newPosition, clearPath=true)

Instantly move the agent to a new position.

Public
turningSpeed

Rotation speed.

Public
updatePosition

Determines if the character's position should be coupled to the Transform's position.

Public
updateRotation

Determines if the character's rotation should be coupled to the Transform's rotation.

Public
velocity

Actual velocity that the agent is moving with.

Public
whenCloseToDestination

What to do when within endReachedDistance units from the destination.

Public

Private/Protected Members

ApplyGravity (deltaTime)

Accelerates the agent downwards.

Protected
Awake ()
Protected
CalculateDeltaToMoveThisFrame (position, distanceToEndOfPath, deltaTime)

Calculates how far to move during a single frame.

Protected
CalculateNextRotation (slowdown, nextRotation)
Protected
CalculatePathRequestEndpoints (start, end)

Outputs the start point and end point of the next automatic path request.

Protected
CalculateTargetPoint (p, a, b)

Calculates target point from the current line segment.

Protected
CalculateVelocity (currentPosition)

Calculates desired velocity.

Protected
CancelCurrentPathRequest ()
Protected
ClampToNavmesh (position, positionChanged)

Constrains the character's position to lie on the navmesh.

Protected
ClearPath ()

Clears the current path of the agent.

Protected
controller

Cached CharacterController component.

Protected
currentWaypointIndex

Current index in the path which is current target.

Protected
interpolator

Helper which calculates points along the current path.

Protected
lastDeltaPosition

Amount which the character wants or tried to move with during the last frame.

Protected
lastDeltaTime

Delta time used for movement during the last frame.

Protected
lastFoundWaypointPosition
Protected
lastFoundWaypointTime
Protected
lastRaycastHit

Hit info from the last raycast done for ground placement.

Protected
lastRepath

Time when the last path request was started.

Protected
minMoveScale
Protected
MovementUpdateInternal (deltaTime, nextPosition, nextRotation)

Called during either Update or FixedUpdate depending on if rigidbodies are used for movement or not.

Protected
OnDisable ()
Protected
OnDrawGizmosSelected ()
Protected
OnEnable ()

Called when the component is enabled.

Protected
OnPathComplete (_p)

Called when a requested path has finished calculation.

Protected
OnUpdate (dt)

Called every frame.

Protected
OnUpgradeSerializedData (version, unityThread)

Handle serialization backwards compatibility.

Protected
path

Current path which is followed.

Protected
prevFrame

Last frame index when prevPosition1 was updated.

Protected
prevPosition1

Position of the character at the end of the last frame.

Protected
prevPosition2

Position of the character at the end of the frame before the last frame.

Protected
RaycastPosition (position, lastElevation)

Checks if the character is grounded and prevents ground penetration.

Protected
Reset ()

Handle serialization backwards compatibility.

Protected
rigid

Cached Rigidbody component.

Protected
rigid2D

Cached Rigidbody component.

Protected
RotateTowards (dir)

Rotates in the specified direction.

Protected
rvoController

Cached RVOController component.

Protected
seeker

Cached Seeker component.

Protected
shouldRecalculatePath

True if the path should be automatically recalculated as soon as possible.

Protected
simulatedPosition

Position of the agent.

Protected
simulatedRotation

Rotation of the agent.

Protected
SimulateRotationTowards (direction, maxDegreesMainAxis, maxDegreesOffAxis=…)

Simulates rotating the agent towards the specified direction and returns the new rotation.

Protected
Start ()

Starts searching for paths.

Protected
targetDirection

Relative direction to where the AI is heading.

Protected
tr

Cached Transform component.

Protected
UpdateMovementPlane ()
Protected
UpdateVelocity ()
Protected
usingGravity

Indicates if gravity is used during this frame.

Protected
velocity2D

Current desired velocity of the agent (does not include local avoidance and physics).

Protected
verticalVelocity

Velocity due to gravity.

Protected
waitingForPathCalculation

Only when the previous path has been calculated should the script consider searching for a new path.

Protected
XZSqrMagnitude (a, b)
Protected