Up to date

This page is up to date for Godot 4.0. If you still find outdated information, please open an issue.

NavigationAgent2D

Inherits: Node < Object

2D Agent used in navigation for collision avoidance.

Description

2D Agent that is used in navigation to reach a position while avoiding static and dynamic obstacles. The dynamic obstacles are avoided using RVO collision avoidance. The agent needs navigation data to work correctly. NavigationAgent2D is physics safe.

Note: After setting target_position it is required to use the get_next_path_position function once every physics frame to update the internal path logic of the NavigationAgent. The returned vector position from this function should be used as the next movement position for the agent’s parent Node.

Tutorials

Properties

bool

avoidance_enabled

false

bool

debug_enabled

false

Color

debug_path_custom_color

Color(1, 1, 1, 1)

float

debug_path_custom_line_width

-1.0

float

debug_path_custom_point_size

4.0

bool

debug_use_custom

false

int

max_neighbors

10

float

max_speed

100.0

int

navigation_layers

1

float

neighbor_distance

500.0

float

path_desired_distance

20.0

float

path_max_distance

100.0

PathMetadataFlags

path_metadata_flags

7

PathPostProcessing

path_postprocessing

0

PathfindingAlgorithm

pathfinding_algorithm

0

float

radius

10.0

float

target_desired_distance

10.0

Vector2

target_position

Vector2(0, 0)

float

time_horizon

1.0

Methods

float

distance_to_target ( ) const

PackedVector2Array

get_current_navigation_path ( ) const

int

get_current_navigation_path_index ( ) const

NavigationPathQueryResult2D

get_current_navigation_result ( ) const

Vector2

get_final_position ( )

bool

get_navigation_layer_value ( int layer_number ) const

RID

get_navigation_map ( ) const

Vector2

get_next_path_position ( )

RID

get_rid ( ) const

bool

is_navigation_finished ( )

bool

is_target_reachable ( )

bool

is_target_reached ( ) const

void

set_navigation_layer_value ( int layer_number, bool value )

void

set_navigation_map ( RID navigation_map )

void

set_velocity ( Vector2 velocity )


Signals

link_reached ( Dictionary details )

Notifies when a navigation link has been reached.

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

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

  • type: Always NavigationPathQueryResult2D.PATH_SEGMENT_TYPE_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.


navigation_finished ( )

Notifies when the final position is reached.


path_changed ( )

Notifies when the navigation path changes.


target_reached ( )

Notifies when the player-defined target_position is reached.


velocity_computed ( Vector2 safe_velocity )

Notifies when the collision avoidance velocity is calculated. Emitted at the end of the physics frame in which set_velocity is called. Only emitted when avoidance_enabled is true.


waypoint_reached ( Dictionary details )

Notifies when a waypoint along the path has been reached.

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

  • 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).


Property Descriptions

bool avoidance_enabled = false

  • void set_avoidance_enabled ( bool value )

  • bool get_avoidance_enabled ( )

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


bool debug_enabled = false

  • void set_debug_enabled ( bool value )

  • bool get_debug_enabled ( )

If true shows debug visuals for this agent.


Color debug_path_custom_color = Color(1, 1, 1, 1)

  • void set_debug_path_custom_color ( Color value )

  • Color get_debug_path_custom_color ( )

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


float debug_path_custom_line_width = -1.0

  • void set_debug_path_custom_line_width ( float value )

  • float get_debug_path_custom_line_width ( )

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


float debug_path_custom_point_size = 4.0

  • void set_debug_path_custom_point_size ( float value )

  • float get_debug_path_custom_point_size ( )

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


bool debug_use_custom = false

  • void set_debug_use_custom ( bool value )

  • bool get_debug_use_custom ( )

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


int max_neighbors = 10

  • void set_max_neighbors ( int value )

  • int get_max_neighbors ( )

The maximum number of neighbors for the agent to consider.


float max_speed = 100.0

  • void set_max_speed ( float value )

  • float get_max_speed ( )

The maximum speed that an agent can move.


int navigation_layers = 1

  • void set_navigation_layers ( int value )

  • int get_navigation_layers ( )

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


float neighbor_distance = 500.0

  • void set_neighbor_distance ( float value )

  • float get_neighbor_distance ( )

The distance to search for other agents.


float path_desired_distance = 20.0

  • void set_path_desired_distance ( float value )

  • float get_path_desired_distance ( )

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


float path_max_distance = 100.0

  • void set_path_max_distance ( float value )

  • float get_path_max_distance ( )

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.


PathMetadataFlags path_metadata_flags = 7

Additional information to return with the navigation path.


PathPostProcessing path_postprocessing = 0

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


PathfindingAlgorithm pathfinding_algorithm = 0

The pathfinding algorithm used in the path query.


float radius = 10.0

  • void set_radius ( float value )

  • float get_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 neighbor_distance).

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


float target_desired_distance = 10.0

  • void set_target_desired_distance ( float value )

  • float get_target_desired_distance ( )

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


Vector2 target_position = Vector2(0, 0)

  • void set_target_position ( Vector2 value )

  • Vector2 get_target_position ( )

The user-defined target position. Setting this property will clear the current navigation path.


float time_horizon = 1.0

  • void set_time_horizon ( float value )

  • float get_time_horizon ( )

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. Must be positive.


Method Descriptions

float distance_to_target ( ) const

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


PackedVector2Array get_current_navigation_path ( ) const

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 get_next_path_position once every physics frame to receive the next path point for the agents movement as this function also updates the internal path logic.


int get_current_navigation_path_index ( ) const

Returns which index the agent is currently on in the navigation path’s PackedVector2Array.


NavigationPathQueryResult2D get_current_navigation_result ( ) const

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


Vector2 get_final_position ( )

Returns the reachable final position in global coordinates. This can change if the navigation path is altered in any way. Because of this, it would be best to check this each frame.


bool get_navigation_layer_value ( int layer_number ) const

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


RID get_navigation_map ( ) const

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 set_navigation_map to change the navigation map for the NavigationAgent and also update the agent on the NavigationServer.


Vector2 get_next_path_position ( )

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.


RID get_rid ( ) const

Returns the RID of this agent on the NavigationServer2D.


bool is_navigation_finished ( )

Returns true if the navigation path’s final position has been reached.


bool is_target_reachable ( )

Returns true if target_position is reachable.


bool is_target_reached ( ) const

Returns true if target_position 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 get_final_position.


void set_navigation_layer_value ( int layer_number, bool value )

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


void set_navigation_map ( RID navigation_map )

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


void set_velocity ( Vector2 velocity )

Sends the passed in velocity to the collision avoidance algorithm. It will adjust the velocity to avoid collisions. Once the adjustment to the velocity is complete, it will emit the velocity_computed signal.