Table of Contents

Class NavigationAgent2D

Namespace
Godot
Assembly
GodotSharp.dll

A 2D agent used to pathfind to a position while avoiding static and dynamic obstacles. The calculation can be used by the parent node to dynamically move it along the path. Requires navigation data to work correctly.

Dynamic obstacles are avoided using RVO collision avoidance. Avoidance is computed before physics, so the pathfinding information can be used safely in the physics step.

Note: After setting the TargetPosition property, the GetNextPathPosition() method must be used once every physics frame to update the internal path logic of the navigation agent. The vector position it returns should be used as the next movement position for the agent's parent node.

public class NavigationAgent2D : Node, IDisposable
Inheritance
NavigationAgent2D
Implements
Inherited Members

Constructors

NavigationAgent2D()

public NavigationAgent2D()

Properties

AvoidanceEnabled

If true the agent is registered for an RVO avoidance callback on the NavigationServer2D. When Velocity is used and the processing is completed a safe_velocity Vector2 is received with a signal connection to VelocityComputed. Avoidance processing with many registered agents has a significant performance cost and should only be enabled on agents that currently require it.

public bool AvoidanceEnabled { get; set; }

Property Value

bool

AvoidanceLayers

A bitfield determining the avoidance layers for this NavigationAgent. Other agents with a matching bit on the AvoidanceMask will avoid this agent.

public uint AvoidanceLayers { get; set; }

Property Value

uint

AvoidanceMask

A bitfield determining what other avoidance agents and obstacles this NavigationAgent will avoid when a bit matches at least one of their AvoidanceLayers.

public uint AvoidanceMask { get; set; }

Property Value

uint

AvoidancePriority

The agent does not adjust the velocity for other agents that would match the AvoidanceMask but have a lower AvoidancePriority. This in turn makes the other agents with lower priority adjust their velocities even more to avoid collision with this agent.

public float AvoidancePriority { get; set; }

Property Value

float

DebugEnabled

If true shows debug visuals for this agent.

public bool DebugEnabled { get; set; }

Property Value

bool

DebugPathCustomColor

If DebugUseCustom is true uses this color for this agent instead of global color.

public Color DebugPathCustomColor { get; set; }

Property Value

Color

DebugPathCustomLineWidth

If DebugUseCustom is true uses this line width for rendering paths for this agent instead of global line width.

public float DebugPathCustomLineWidth { get; set; }

Property Value

float

DebugPathCustomPointSize

If DebugUseCustom is true uses this rasterized point size for rendering path points for this agent instead of global point size.

public float DebugPathCustomPointSize { get; set; }

Property Value

float

DebugUseCustom

If true uses the defined DebugPathCustomColor for this agent instead of global color.

public bool DebugUseCustom { get; set; }

Property Value

bool

MaxNeighbors

The maximum number of neighbors for the agent to consider.

public int MaxNeighbors { get; set; }

Property Value

int

MaxSpeed

The maximum speed that an agent can move.

public float MaxSpeed { get; set; }

Property Value

float

NavigationLayers

A bitfield determining which navigation layers of navigation regions this agent will use to calculate a path. Changing it during runtime will clear the current navigation path and generate a new one, according to the new navigation layers.

public uint NavigationLayers { get; set; }

Property Value

uint

NeighborDistance

The distance to search for other agents.

public float NeighborDistance { get; set; }

Property Value

float

PathDesiredDistance

The distance threshold before a path point is considered to be reached. This allows agents to not have to hit a path point on the path exactly, but only to reach its general area. If this value is set too high, the NavigationAgent will skip points on the path, which can lead to leaving the navigation mesh. If this value is set too low, the NavigationAgent will be stuck in a repath loop because it will constantly overshoot or undershoot the distance to the next point on each physics frame update.

public float PathDesiredDistance { get; set; }

Property Value

float

PathMaxDistance

The maximum distance the agent is allowed away from the ideal path to the final position. This can happen due to trying to avoid collisions. When the maximum distance is exceeded, it recalculates the ideal path.

public float PathMaxDistance { get; set; }

Property Value

float

PathMetadataFlags

Additional information to return with the navigation path.

public NavigationPathQueryParameters2D.PathMetadataFlags PathMetadataFlags { get; set; }

Property Value

NavigationPathQueryParameters2D.PathMetadataFlags

PathPostprocessing

The path postprocessing applied to the raw path corridor found by the PathfindingAlgorithm.

public NavigationPathQueryParameters2D.PathPostProcessing PathPostprocessing { get; set; }

Property Value

NavigationPathQueryParameters2D.PathPostProcessing

PathfindingAlgorithm

The pathfinding algorithm used in the path query.

public NavigationPathQueryParameters2D.PathfindingAlgorithmEnum PathfindingAlgorithm { get; set; }

Property Value

NavigationPathQueryParameters2D.PathfindingAlgorithmEnum

Radius

The radius of the avoidance agent. This is the "body" of the avoidance agent and not the avoidance maneuver starting radius (which is controlled by NeighborDistance).

Does not affect normal pathfinding. To change an actor's pathfinding radius bake NavigationMesh resources with a different AgentRadius property and use different navigation maps for each actor size.

public float Radius { get; set; }

Property Value

float

TargetDesiredDistance

The distance threshold before the final target point is considered to be reached. This allows agents to not have to hit the point of the final target exactly, but only to reach its general area. If this value is set too low, the NavigationAgent will be stuck in a repath loop because it will constantly overshoot or undershoot the distance to the final target point on each physics frame update.

public float TargetDesiredDistance { get; set; }

Property Value

float

TargetPosition

If set, a new navigation path from the current agent position to the TargetPosition is requested from the NavigationServer.

public Vector2 TargetPosition { get; set; }

Property Value

Vector2

TimeHorizonAgents

The minimal amount of time for which this agent's velocities, that are computed with the collision avoidance algorithm, are safe with respect to other agents. The larger the number, the sooner the agent will respond to other agents, but less freedom in choosing its velocities. A too high value will slow down agents movement considerably. Must be positive.

public float TimeHorizonAgents { get; set; }

Property Value

float

TimeHorizonObstacles

The minimal amount of time for which this agent's velocities, that are computed with the collision avoidance algorithm, are safe with respect to static avoidance obstacles. The larger the number, the sooner the agent will respond to static avoidance obstacles, but less freedom in choosing its velocities. A too high value will slow down agents movement considerably. Must be positive.

public float TimeHorizonObstacles { get; set; }

Property Value

float

Velocity

Sets the new wanted velocity for the agent. The avoidance simulation will try to fulfill this velocity if possible but will modify it to avoid collision with other agents and obstacles. When an agent is teleported to a new position, use SetVelocityForced(Vector2) as well to reset the internal simulation velocity.

public Vector2 Velocity { get; set; }

Property Value

Vector2

Methods

DistanceToTarget()

Returns the distance to the target position, using the agent's global position. The user must set TargetPosition in order for this to be accurate.

public float DistanceToTarget()

Returns

float

GetAvoidanceLayerValue(int)

Returns whether or not the specified layer of the AvoidanceLayers bitmask is enabled, given a layerNumber between 1 and 32.

public bool GetAvoidanceLayerValue(int layerNumber)

Parameters

layerNumber int

Returns

bool

GetAvoidanceMaskValue(int)

Returns whether or not the specified mask of the AvoidanceMask bitmask is enabled, given a maskNumber between 1 and 32.

public bool GetAvoidanceMaskValue(int maskNumber)

Parameters

maskNumber int

Returns

bool

GetCurrentNavigationPath()

Returns this agent's current path from start to finish in global coordinates. The path only updates when the target position is changed or the agent requires a repath. The path array is not intended to be used in direct path movement as the agent has its own internal path logic that would get corrupted by changing the path array manually. Use the intended GetNextPathPosition() once every physics frame to receive the next path point for the agents movement as this function also updates the internal path logic.

public Vector2[] GetCurrentNavigationPath()

Returns

Vector2[]

GetCurrentNavigationPathIndex()

Returns which index the agent is currently on in the navigation path's Vector2[].

public int GetCurrentNavigationPathIndex()

Returns

int

GetCurrentNavigationResult()

Returns the path query result for the path the agent is currently following.

public NavigationPathQueryResult2D GetCurrentNavigationResult()

Returns

NavigationPathQueryResult2D

GetFinalPosition()

Returns the reachable final position of the current navigation path in global coordinates. This position can change if the agent needs to update the navigation path which makes the agent emit the PathChanged signal.

public Vector2 GetFinalPosition()

Returns

Vector2

GetNavigationLayerValue(int)

Returns whether or not the specified layer of the NavigationLayers bitmask is enabled, given a layerNumber between 1 and 32.

public bool GetNavigationLayerValue(int layerNumber)

Parameters

layerNumber int

Returns

bool

GetNavigationMap()

Returns the Rid of the navigation map for this NavigationAgent node. This function returns always the map set on the NavigationAgent node and not the map of the abstract agent on the NavigationServer. If the agent map is changed directly with the NavigationServer API the NavigationAgent node will not be aware of the map change. Use SetNavigationMap(Rid) to change the navigation map for the NavigationAgent and also update the agent on the NavigationServer.

public Rid GetNavigationMap()

Returns

Rid

GetNextPathPosition()

Returns the next position in global coordinates that can be moved to, making sure that there are no static objects in the way. If the agent does not have a navigation path, it will return the position of the agent's parent. The use of this function once every physics frame is required to update the internal path logic of the NavigationAgent.

public Vector2 GetNextPathPosition()

Returns

Vector2

GetRid()

Returns the Rid of this agent on the NavigationServer2D.

public Rid GetRid()

Returns

Rid

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

IsNavigationFinished()

Returns true if the end of the currently loaded navigation path has been reached.

Note: While true prefer to stop calling update functions like GetNextPathPosition(). This avoids jittering the standing agent due to calling repeated path updates.

public bool IsNavigationFinished()

Returns

bool

IsTargetReachable()

Returns true if GetFinalPosition() is within TargetDesiredDistance of the TargetPosition.

public bool IsTargetReachable()

Returns

bool

IsTargetReached()

Returns true if TargetPosition is reached. It may not always be possible to reach the target position. It should always be possible to reach the final position though. See GetFinalPosition().

public bool IsTargetReached()

Returns

bool

SetAvoidanceLayerValue(int, bool)

Based on value, enables or disables the specified layer in the AvoidanceLayers bitmask, given a layerNumber between 1 and 32.

public void SetAvoidanceLayerValue(int layerNumber, bool value)

Parameters

layerNumber int
value bool

SetAvoidanceMaskValue(int, bool)

Based on value, enables or disables the specified mask in the AvoidanceMask bitmask, given a maskNumber between 1 and 32.

public void SetAvoidanceMaskValue(int maskNumber, bool value)

Parameters

maskNumber int
value bool

SetNavigationLayerValue(int, bool)

Based on value, enables or disables the specified layer in the NavigationLayers bitmask, given a layerNumber between 1 and 32.

public void SetNavigationLayerValue(int layerNumber, bool value)

Parameters

layerNumber int
value bool

SetNavigationMap(Rid)

Sets the Rid of the navigation map this NavigationAgent node should use and also updates the agent on the NavigationServer.

public void SetNavigationMap(Rid navigationMap)

Parameters

navigationMap Rid

SetVelocityForced(Vector2)

Replaces the internal velocity in the collision avoidance simulation with velocity. When an agent is teleported to a new position this function should be used in the same frame. If called frequently this function can get agents stuck.

public void SetVelocityForced(Vector2 velocity)

Parameters

velocity Vector2

Events

LinkReached

Notifies when a navigation link has been reached.

The details dictionary may contain the following keys depending on the value of PathMetadataFlags:

- position: The start position of the link that was reached.

- type: Always Link.

- rid: The Rid of the link.

- owner: The object which manages the link (usually NavigationLink2D).

- link_entry_position: If owner is available and the owner is a NavigationLink2D, it will contain the global position of the link's point the agent is entering.

- link_exit_position: If owner is available and the owner is a NavigationLink2D, it will contain the global position of the link's point which the agent is exiting.

public event NavigationAgent2D.LinkReachedEventHandler LinkReached

Event Type

NavigationAgent2D.LinkReachedEventHandler

NavigationFinished

Emitted once per loaded path when the agent internal navigation path index reaches the last index of the loaded path array. The agent internal navigation path index can be received with GetCurrentNavigationPathIndex().

public event Action NavigationFinished

Event Type

Action

PathChanged

Emitted when the agent had to update the loaded path:

- because path was previously empty.

- because navigation map has changed.

- because agent pushed further away from the current path segment than the PathMaxDistance.

public event Action PathChanged

Event Type

Action

TargetReached

Emitted once per loaded path when the agent's global position is the first time within TargetDesiredDistance to the TargetPosition.

public event Action TargetReached

Event Type

Action

VelocityComputed

Notifies when the collision avoidance velocity is calculated. Emitted when Velocity is set. Only emitted when AvoidanceEnabled is true.

public event NavigationAgent2D.VelocityComputedEventHandler VelocityComputed

Event Type

NavigationAgent2D.VelocityComputedEventHandler

WaypointReached

Notifies when a waypoint along the path has been reached.

The details dictionary may contain the following keys depending on the value of PathMetadataFlags:

- position: The position of the waypoint that was reached.

- type: The type of navigation primitive (region or link) that contains this waypoint.

- rid: The Rid of the containing navigation primitive (region or link).

- owner: The object which manages the containing navigation primitive (region or link).

public event NavigationAgent2D.WaypointReachedEventHandler WaypointReached

Event Type

NavigationAgent2D.WaypointReachedEventHandler