A* Pathfinding Project  4.1.0
The A* Pathfinding Project for Unity 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Events Macros Groups Pages
AILerp Class Reference

Linearly interpolating movement script. More...

Detailed Description

Linearly interpolating movement script.

This movement script will follow the path exactly, it uses linear interpolation to move between the waypoints in the path. This is desirable for some types of games. It also works in 2D.

Recommended setup:

This depends on what type of movement you are aiming for. If you are aiming for movement where the unit follows the path exactly (you are likely using a grid or point graph) the default settings on this component should work quite well, however I recommend that you adjust the StartEndModifier on the Seeker component: set the 'Exact Start Point' field to 'NodeConnection' and the 'Exact End Point' field to 'SnapToNode'.

If you on the other hand want smoother movement I recommend adding the Simple Smooth Modifier to the GameObject as well. You may also want to tweak the rotationSpeed.

Public Member Functions

virtual void ForceSearchPath ()
 Requests a path to the target.
 
virtual Vector3 GetFeetPosition ()
 
void MovementUpdate (float deltaTime)
 Calculate all movement for a single movement step and move the character.
 
void OnDisable ()
 
virtual void OnPathComplete (Path _p)
 Called when a requested path has finished calculation.
 
virtual void OnTargetReached ()
 The end of the path has been reached.
 
virtual void SearchPath ()
 Requests a path to the target.
 
float TrySearchPath ()
 Tries to search for a path.
 
- Public Member Functions inherited from IAstarAI
void MovementUpdate (float deltaTime, bool useGravity)
 Calculate all movement for a single movement step and move the agent.
 
void SearchPath ()
 Recalculate the current path.
 
void Teleport (Vector3 newPosition, bool clearPath=true)
 Instantly move the agent to a new position.
 

Public Attributes

bool canMove = true
 Enables or disables movement completely.
 
bool canSearch = true
 Enables or disables recalculating the path at regular intervals.
 
bool enableRotation = true
 If true, the AI will rotate to face the movement direction.
 
bool interpolatePathSwitches = true
 If true, some interpolation will be done when a new path has been calculated.
 
float repathRate = 0.5F
 Determines how often it will search for new paths.
 
bool rotationIn2D = false
 If true, rotation will only be done along the Z axis so that the Y axis is the forward direction of the character.
 
float rotationSpeed = 10
 How quickly to rotate.
 
float speed = 3
 Speed in world units.
 
float switchPathInterpolationSpeed = 5
 How quickly to interpolate to the new path.
 

Protected Member Functions

override void Awake ()
 Initializes reference variables.
 
virtual Vector3 CalculateNextPosition (out Vector3 direction)
 Calculate the AI's next position (one frame in the future).
 
virtual void ConfigureNewPath ()
 Finds the closest point on the current path and configures the interpolator.
 
virtual void ConfigurePathSwitchInterpolation ()
 
virtual void OnEnable ()
 Called when the component is enabled.
 
override int OnUpgradeSerializedData (int version, bool unityThread)
 Handle serialization backwards compatibility.
 
IEnumerator RepeatTrySearchPath ()
 Tries to search for a path every repathRate seconds.
 
virtual void Start ()
 Starts searching for paths.
 
virtual void Update ()
 

Protected Attributes

bool canSearchAgain = true
 Only when the previous path has been returned should a search for a new path be done.
 
PathInterpolator interpolator = new PathInterpolator()
 
float lastRepath = -9999
 Time when the last path request was sent.
 
ABPath path
 Current path which is followed.
 
Vector3 previousMovementDirection
 
Vector3 previousMovementOrigin
 When a new path was returned, the AI was moving along this ray.
 
float previousMovementStartTime = -9999
 
Seeker seeker
 Cached Seeker component.
 
Transform tr
 Cached Transform component.
 

Properties

bool IAstarAI. canMove [get, set]
 Enables or disables movement completely.
 
bool IAstarAI. canSearch [get, set]
 Enables or disables recalculating the path at regular intervals.
 
Vector3 IAstarAI. desiredVelocity [get]
 
Vector3 destination [get, set]
 
bool hasPath [get]
 
bool isStopped [get, set]
 Gets or sets if the agent should stop moving.
 
float IAstarAI. maxSpeed [get, set]
 
System.Action onSearchPath [get, set]
 
bool pathPending [get]
 
Vector3 IAstarAI. position [get]
 Position of the agent.
 
float remainingDistance [get, set]
 
Vector3 IAstarAI. steeringTarget [get]
 Point on the path which the agent is currently moving towards.
 
Transform target [get, set]
 Target to move towards.
 
bool targetReached [get, set]
 True if the end of the current path has been reached.
 
Vector3 IAstarAI. velocity [get]
 
- Properties inherited from IAstarAI
bool canMove [get, set]
 Enables or disables movement completely.
 
bool canSearch [get, set]
 Enables or disables recalculating the path at regular intervals.
 
Vector3 desiredVelocity [get]
 Velocity that this agent wants to move with.
 
Vector3 destination [get, set]
 Position in the world that this agent should move to.
 
bool hasPath [get]
 True if this agent currently has a path that it follows.
 
bool isStopped [get, set]
 Gets or sets if the agent should stop moving.
 
float maxSpeed [get, set]
 Max speed in world units per second.
 
System.Action onSearchPath [get, set]
 Called when the agent recalculates its path.
 
bool pathPending [get]
 True if a path is currently being calculated.
 
Vector3 position [get]
 Position of the agent.
 
float remainingDistance [get]
 Remaining distance along the current path to the destination.
 
Vector3 steeringTarget [get]
 Point on the path which the agent is currently moving towards.
 
bool targetReached [get]
 True if the agent has reached the end of the current path.
 
Vector3 velocity [get]
 Actual velocity that the agent is moving with.
 

Private Member Functions

void Init ()
 
void IAstarAI. MovementUpdate (float deltaTime, bool useGravity)
 
void RotateTowards (Vector3 direction)
 
void IAstarAI. Teleport (Vector3 position, bool clearPath)
 

Private Attributes

Vector3 previousPosition1
 
Vector3 previousPosition2
 
bool startHasRun = false
 Holds if the Start function has been run.
 
Transform targetCompatibility
 Required for serialization backward compatibility.
 

Member Function Documentation

override void Awake ( )
protectedvirtual

Initializes reference variables.

If you override this function you should in most cases call base.Awake () at the start of it.

Reimplemented from VersionedMonoBehaviour.

virtual Vector3 CalculateNextPosition ( out Vector3  direction)
protectedvirtual

Calculate the AI's next position (one frame in the future).

Parameters
directionThe tangent of the segment the AI is currently traversing. Not normalized.
virtual void ConfigureNewPath ( )
protectedvirtual

Finds the closest point on the current path and configures the interpolator.

virtual void ConfigurePathSwitchInterpolation ( )
protectedvirtual
virtual void ForceSearchPath ( )
virtual

Requests a path to the target.

Bypasses 'is-it-a-good-time-to-request-a-path' checks.

virtual Vector3 GetFeetPosition ( )
virtual
void Init ( )
private
void IAstarAI. MovementUpdate ( float  deltaTime,
bool  useGravity 
)
private
void MovementUpdate ( float  deltaTime)

Calculate all movement for a single movement step and move the character.

This is called automatically unless canMove is false.

void OnDisable ( )
virtual void OnEnable ( )
protectedvirtual

Called when the component is enabled.

virtual void OnPathComplete ( Path  _p)
virtual

Called when a requested path has finished calculation.

A path is first requested by SearchPath, 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.

virtual void OnTargetReached ( )
virtual

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.

override int OnUpgradeSerializedData ( int  version,
bool  unityThread 
)
protectedvirtual

Handle serialization backwards compatibility.

Reimplemented from VersionedMonoBehaviour.

IEnumerator RepeatTrySearchPath ( )
protected

Tries to search for a path every repathRate seconds.

See Also
TrySearchPath
void RotateTowards ( Vector3  direction)
private
virtual void SearchPath ( )
virtual

Requests a path to the target.

Some inheriting classes will prevent the path from being requested immediately when this function is called, for example when the AI is currently traversing a special path segment in which case it is usually a bad idea to search for a new path.

virtual void Start ( )
protectedvirtual

Starts searching for paths.

If you override this function you should in most cases call base.Start () at the start of it.

See Also
Init
RepeatTrySearchPath
void IAstarAI. Teleport ( Vector3  position,
bool  clearPath 
)
private
float TrySearchPath ( )

Tries to search for a path.

Will search for a new path if there was a sufficient time since the last repath and both canSearchAgain and canSearch are true and there is a target.

Returns
The time to wait until calling this function again (based on repathRate)
virtual void Update ( )
protectedvirtual

Member Data Documentation

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.

See Also
canSearch
isStopped

bool canSearch = true

Enables or disables recalculating the path at regular intervals.

Setting this to false does not stop any active path requests from being calculated or stop it from continuing to follow the current path.Note that this only disables automatic path recalculations. If you call the SearchPath() method a path will still be calculated.

See Also
canMove
isStopped

bool canSearchAgain = true
protected

Only when the previous path has been returned should a search for a new path be done.

bool enableRotation = true

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

bool interpolatePathSwitches = true

If true, some interpolation will be done when a new path has been calculated.

This is used to avoid short distance teleportation.

PathInterpolator interpolator = new PathInterpolator()
protected
float lastRepath = -9999
protected

Time when the last path request was sent.

ABPath path
protected

Current path which is followed.

Vector3 previousMovementDirection
protected
Vector3 previousMovementOrigin
protected

When a new path was returned, the AI was moving along this ray.

Used to smoothly interpolate between the previous movement and the movement along the new path. The speed is equal to movement direction.

float previousMovementStartTime = -9999
protected
Vector3 previousPosition1
private
Vector3 previousPosition2
private
float repathRate = 0.5F

Determines how often it will search for new paths.

If you have fast moving targets or AIs, you might want to set it to a lower value. The value is in seconds between path requests.

bool rotationIn2D = false

If true, rotation will only be done along the Z axis so that the Y axis is the forward direction of the character.

This is useful for 2D games in which one often want to have the Y axis as the forward direction to get sprites and 2D colliders to work properly.

float rotationSpeed = 10

How quickly to rotate.

Seeker seeker
protected

Cached Seeker component.

float speed = 3

Speed in world units.

bool startHasRun = false
private

Holds if the Start function has been run.

Used to test if coroutines should be started in OnEnable to prevent calculating paths in the awake stage (or rather before start on frame 0).

float switchPathInterpolationSpeed = 5

How quickly to interpolate to the new path.

Transform targetCompatibility
private

Required for serialization backward compatibility.

Transform tr
protected

Cached Transform component.

Property Documentation

bool IAstarAI. canMove
getsetprivate

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.

See Also
canSearch
isStopped

bool IAstarAI. canSearch
getsetprivate

Enables or disables recalculating the path at regular intervals.

Setting this to false does not stop any active path requests from being calculated or stop it from continuing to follow the current path.Note that this only disables automatic path recalculations. If you call the SearchPath() method a path will still be calculated.

See Also
canMove
isStopped

Vector3 IAstarAI. desiredVelocity
getprivate
Vector3 destination
getset
bool hasPath
get
bool isStopped
getset

Gets or sets if the agent should stop moving.

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 targetReached for that.If this property is set to false 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).

The steeringTarget property will continue to indicate the point which the agent would move towards if it would not be stopped.

float IAstarAI. maxSpeed
getsetprivate
System.Action onSearchPath
getset
bool pathPending
get
Vector3 IAstarAI. position
getprivate

Position of the agent.

In world space.

See Also
Teleport

float remainingDistance
getset
Vector3 IAstarAI. steeringTarget
getprivate

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

This is usually a point a small distance ahead of the agent or the end of the path.If the agent does not have a path at the moment, then the agent's current position will be returned.

Transform target
getset

Target to move towards.

The AI will try to follow/move towards this target. 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.

Deprecated:
In 4.0 this will automatically add a AIDestinationSetter component and set the target on that component. Try instead to use the destination property which does not require a transform to be created as the target or use the AIDestinationSetter component directly.
bool targetReached
getset

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

Vector3 IAstarAI. velocity
getprivate

The documentation for this class was generated from the following file: