NavigationAgent2D
A 2D agent used to pathfind to a position while avoiding obstacles.
A 2D agent used to pathfind to a position while avoiding obstacles.
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 NavigationAgent2D.TargetPosition property, the NavigationAgent2D.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.
Note: Several methods of this class, such as NavigationAgent2D.GetNextPathPosition, can trigger a new path calculation. Calling these in your callback to an agent's signal, such as NavigationAgent2D.WaypointReached, can cause infinite recursion. It is recommended to call these methods in the physics step or, alternatively, delay their call until the end of the frame (see ReckfellObject.CallDeferred or ReckfellObject.ConnectFlags.Deferred).
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
NavigationAgent2DConstructor
public NavigationAgent2D();Properties30
AvoidanceEnabledProperty
public bool AvoidanceEnabled { get; set; }Enables avoidance computation. Submit desired Velocity and apply VelocityComputed output to the body. The flag does not move it or guarantee avoiding every obstacle.
AvoidanceLayersProperty
public uint AvoidanceLayers { get; set; }A bitfield determining the avoidance layers for this NavigationAgent. Other agents with a matching bit on the NavigationAgent2D.AvoidanceMask will avoid this agent.
AvoidanceMaskProperty
public uint AvoidanceMask { get; set; }A bitfield determining what other avoidance agents and obstacles this NavigationAgent will avoid when a bit matches at least one of their NavigationAgent2D.AvoidanceLayers.
AvoidancePriorityProperty
public float AvoidancePriority { get; set; }The agent does not adjust the velocity for other agents that would match the NavigationAgent2D.AvoidanceMask but have a lower NavigationAgent2D.AvoidancePriority. This in turn makes the other agents with lower priority adjust their velocities even more to avoid collision with this agent.
DebugEnabledProperty
public bool DebugEnabled { get; set; }If true shows debug visuals for this agent.
DebugPathCustomColorProperty
public Color DebugPathCustomColor { get; set; }If NavigationAgent2D.DebugUseCustom is true uses this color for this agent instead of global color.
DebugPathCustomLineWidthProperty
public float DebugPathCustomLineWidth { get; set; }If NavigationAgent2D.DebugUseCustom is true uses this line width for rendering paths for this agent instead of global line width.
DebugPathCustomPointSizeProperty
public float DebugPathCustomPointSize { get; set; }If NavigationAgent2D.DebugUseCustom is true uses this rasterized point size for rendering path points for this agent instead of global point size.
DebugUseCustomProperty
public bool DebugUseCustom { get; set; }If true uses the defined NavigationAgent2D.DebugPathCustomColor for this agent instead of global color.
MaxNeighborsProperty
public int MaxNeighbors { get; set; }The maximum number of neighbors for the agent to consider.
MaxSpeedProperty
public float MaxSpeed { get; set; }Avoidance-computation speed limit. Align it with the body desired speed; changing this property does not automatically move the body.
NavigationLayersProperty
public uint NavigationLayers { get; set; }Mask of surfaces and links for the agent route. Any shared bit admits a region; this is not an all-keys condition. Changing it with an active goal requests a new path, as checked with 1 → 0 → 1.
NeighborDistanceProperty
public float NeighborDistance { get; set; }The distance to search for other agents.
PathDesiredDistanceProperty
public float PathDesiredDistance { get; set; }Distance tolerance for approaching the next waypoint in map units. Tune for speed and physics step: too small a tolerance can cause overshooting and backtracking.
PathMaxDistanceProperty
public float PathMaxDistance { get; set; }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.
PathMetadataFlagsProperty
public NavigationPathQueryParameters2D.PathMetadataFlags PathMetadataFlags { get; set; }Additional information to return with the navigation path.
PathPostprocessingProperty
public NavigationPathQueryParameters2D.PathPostProcessing PathPostprocessing { get; set; }The path postprocessing applied to the raw path corridor found by the NavigationAgent2D.PathfindingAlgorithm.
PathReturnMaxLengthProperty
public float PathReturnMaxLength { get; set; }The maximum allowed length of the returned path in world units. A path will be clipped when going over this length.
PathReturnMaxRadiusProperty
public float PathReturnMaxRadius { get; set; }The maximum allowed radius in world units that the returned path can be from the path start. The path will be clipped when going over this radius. Compared to NavigationAgent2D.PathReturnMaxLength, this allows the agent to go that much further, if they need to walk around a corner.
Note: This will perform a sphere clip considering only the actual navigation mesh path points with the first path position being the sphere's center.
PathSearchMaxDistanceProperty
public float PathSearchMaxDistance { get; set; }The maximum distance a searched polygon can be away from the start polygon before the pathfinding cancels the search for a path to the (possibly unreachable or very far away) target position polygon. In this case the pathfinding resets and builds a path from the start polygon to the polygon that was found closest to the target position so far. A value of 0 or below counts as unlimited. In case of unlimited the pathfinding will search all polygons connected with the start polygon until either the target position polygon is found or all available polygon search options are exhausted.
PathSearchMaxPolygonsProperty
public int PathSearchMaxPolygons { get; set; }The maximum number of polygons that are searched before the pathfinding cancels the search for a path to the (possibly unreachable or very far away) target position polygon. In this case the pathfinding resets and builds a path from the start polygon to the polygon that was found closest to the target position so far. A value of 0 or below counts as unlimited. In case of unlimited the pathfinding will search all polygons connected with the start polygon until either the target position polygon is found or all available polygon search options are exhausted.
PathfindingAlgorithmProperty
public NavigationPathQueryParameters2D.PathfindingAlgorithmEnum PathfindingAlgorithm { get; set; }The pathfinding algorithm used in the path query.
RadiusProperty
public float Radius { get; set; }Agent circle radius for avoidance. Does not change a physics collider or rebake navigation geometry for a different character size.
SimplifyEpsilonProperty
public float SimplifyEpsilon { get; set; }The path simplification amount in world units.
SimplifyPathProperty
public bool SimplifyPath { get; set; }If true a simplified version of the path will be returned with less critical path points removed. The simplification amount is controlled by NavigationAgent2D.SimplifyEpsilon. The simplification uses a variant of Ramer-Douglas-Peucker algorithm for curve point decimation.
Path simplification can be helpful to mitigate various path following issues that can arise with certain agent types and script behaviors. E.g. "steering" agents or avoidance in "open fields".
TargetDesiredDistanceProperty
public float TargetDesiredDistance { get; set; }Tolerance for reaching the original goal and comparing the found endpoint against it. A large value may finish a route earlier than intended.
TargetPositionProperty
public Vector2 TargetPosition { get; set; }Global agent goal. Assignment requests a new route; avoid assigning the same goal every frame without a reason. The agent does not automatically move its parent.
TimeHorizonAgentsProperty
public float TimeHorizonAgents { get; set; }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 agent movement considerably. Must be positive.
TimeHorizonObstaclesProperty
public float TimeHorizonObstacles { get; set; }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 agent movement considerably. Must be positive.
VelocityProperty
public Vector2 Velocity { get; set; }Desired avoidance velocity in map units per second. Do not multiply it by delta when subsequently supplying it to CharacterBody2D.MoveAndSlide. Computed velocity arrives through VelocityComputed.
Methods80
DistanceToTargetfloat
public float DistanceToTarget();Returns the distance to the target position, using the agent's global position. The user must set NavigationAgent2D.TargetPosition in order for this to be accurate.
floatGetAvoidanceEnabledbool
public bool GetAvoidanceEnabled();boolGetAvoidanceLayerValuebool
public bool GetAvoidanceLayerValue(int layerNumber);Returns whether or not the specified layer of the NavigationAgent2D.AvoidanceLayers bitmask is enabled, given a layer_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
layerNumber | int | — |
boolGetAvoidanceLayersuint
public uint GetAvoidanceLayers();uintGetAvoidanceMaskuint
public uint GetAvoidanceMask();uintGetAvoidanceMaskValuebool
public bool GetAvoidanceMaskValue(int maskNumber);Returns whether or not the specified mask of the NavigationAgent2D.AvoidanceMask bitmask is enabled, given a mask_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
maskNumber | int | — |
boolGetAvoidancePriorityfloat
public float GetAvoidancePriority();floatGetCurrentNavigationPathVector2[]
public Vector2[] GetCurrentNavigationPath();Reads the agent current path array. Does not move the body; points may end at an accessible boundary rather than the original goal.
Vector2[]Vector2[] containing the current result; check Length before accessing the first or last point.
GetCurrentNavigationPathIndexint
public int GetCurrentNavigationPathIndex();Returns which index the agent is currently on in the navigation path's Vector2[].
intGetCurrentNavigationResultNavigationPathQueryResult2D
public NavigationPathQueryResult2D GetCurrentNavigationResult();Returns the path query result for the path the agent is currently following.
NavigationPathQueryResult2DGetDebugEnabledbool
public bool GetDebugEnabled();boolGetDebugPathCustomColorColor
public Color GetDebugPathCustomColor();ColorGetDebugPathCustomLineWidthfloat
public float GetDebugPathCustomLineWidth();floatGetDebugPathCustomPointSizefloat
public float GetDebugPathCustomPointSize();floatGetDebugUseCustombool
public bool GetDebugUseCustom();boolGetFinalPositionVector2
public Vector2 GetFinalPosition();Updates navigation and returns the current path endpoint, which may differ from the original goal.
Vector2Last path point, or Vector2.Zero for an empty result. A zero vector alone is not a reliable path-existence test.
GetMaxNeighborsint
public int GetMaxNeighbors();intGetMaxSpeedfloat
public float GetMaxSpeed();floatGetNavigationLayerValuebool
public bool GetNavigationLayerValue(int layerNumber);Returns whether or not the specified layer of the NavigationAgent2D.NavigationLayers bitmask is enabled, given a layer_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
layerNumber | int | — |
boolGetNavigationLayersuint
public uint GetNavigationLayers();Reads the agent query mask. Does not describe body size or avoidance radius.
uintCurrent NavigationLayers mask; zero admits no layer for a new path.
GetNavigationMapRid
public Rid GetNavigationMap();Returns the explicitly assigned agent map or, without one, its valid parent world map. The locally assigned Entity map can change before the server queue is processed.
RidRID of the selected map; may be zero without an assigned map and suitable parent.
GetNeighborDistancefloat
public float GetNeighborDistance();floatGetNextPathPositionVector2
public Vector2 GetNextPathPosition();Updates agent state and returns the next point. Use it in the physics loop for your own parent movement; stop following after route completion.
Vector2Global path point; with an empty path and a valid parent, returns that parent position rather than a separate error code.
GetPathDesiredDistancefloat
public float GetPathDesiredDistance();floatGetPathLengthfloat
public float GetPathLength();Returns the length of the currently calculated path. The returned value is 0.0, if the path is still calculating or no calculation has been requested yet.
floatGetPathMaxDistancefloat
public float GetPathMaxDistance();floatGetPathMetadataFlagsNavigationPathQueryParameters2D.PathMetadataFlags
public NavigationPathQueryParameters2D.PathMetadataFlags GetPathMetadataFlags();NavigationPathQueryParameters2D.PathMetadataFlagsGetPathPostprocessingNavigationPathQueryParameters2D.PathPostProcessing
public NavigationPathQueryParameters2D.PathPostProcessing GetPathPostprocessing();NavigationPathQueryParameters2D.PathPostProcessingGetPathReturnMaxLengthfloat
public float GetPathReturnMaxLength();floatGetPathReturnMaxRadiusfloat
public float GetPathReturnMaxRadius();floatGetPathSearchMaxDistancefloat
public float GetPathSearchMaxDistance();floatGetPathSearchMaxPolygonsint
public int GetPathSearchMaxPolygons();intGetPathfindingAlgorithmNavigationPathQueryParameters2D.PathfindingAlgorithmEnum
public NavigationPathQueryParameters2D.PathfindingAlgorithmEnum GetPathfindingAlgorithm();NavigationPathQueryParameters2D.PathfindingAlgorithmEnumGetRadiusfloat
public float GetRadius();floatGetRidRid
public Rid GetRid();Returns the Rid of this agent on the NavigationServer2D.
RidGetSimplifyEpsilonfloat
public float GetSimplifyEpsilon();floatGetSimplifyPathbool
public bool GetSimplifyPath();boolGetTargetDesiredDistancefloat
public float GetTargetDesiredDistance();floatGetTargetPositionVector2
public Vector2 GetTargetPosition();Vector2GetTimeHorizonAgentsfloat
public float GetTimeHorizonAgents();floatGetTimeHorizonObstaclesfloat
public float GetTimeHorizonObstacles();floatGetVelocityVector2
public Vector2 GetVelocity();Vector2IsNavigationFinishedbool
public bool IsNavigationFinished();Updates navigation state and reports whether the current route finished. A partial path can finish too: check IsTargetReached separately.
booltrue for a finished route; not proof that the original goal was reached.
IsTargetReachablebool
public bool IsTargetReachable();Updates navigation and compares the found endpoint with the goal using TargetDesiredDistance. Check that a current path exists; this does not guarantee physical-body traversal through the level.
boolComparison of the current route endpoint with the goal. Returns false for the checked off-map goal.
IsTargetReachedbool
public bool IsTargetReached();Reads the stored goal-reached state using TargetDesiredDistance. Path-following logic updates it; this is not a direct physics reachability test.
booltrue when the agent marked the original goal reached; finishing a partial path can leave it false.
SetAvoidanceEnabledvoid
public void SetAvoidanceEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetAvoidanceLayerValuevoid
public void SetAvoidanceLayerValue(int layerNumber, bool value);Based on value, enables or disables the specified layer in the NavigationAgent2D.AvoidanceLayers bitmask, given a layer_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
layerNumber | int | — |
value | bool | — |
SetAvoidanceLayersvoid
public void SetAvoidanceLayers(uint layers);| Name | Type | Default |
|---|---|---|
layers | uint | — |
SetAvoidanceMaskvoid
public void SetAvoidanceMask(uint mask);| Name | Type | Default |
|---|---|---|
mask | uint | — |
SetAvoidanceMaskValuevoid
public void SetAvoidanceMaskValue(int maskNumber, bool value);Based on value, enables or disables the specified mask in the NavigationAgent2D.AvoidanceMask bitmask, given a mask_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
maskNumber | int | — |
value | bool | — |
SetAvoidancePriorityvoid
public void SetAvoidancePriority(float priority);| Name | Type | Default |
|---|---|---|
priority | float | — |
SetDebugEnabledvoid
public void SetDebugEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetDebugPathCustomColorvoid
public void SetDebugPathCustomColor(Color color);| Name | Type | Default |
|---|---|---|
color | Color | — |
SetDebugPathCustomLineWidthvoid
public void SetDebugPathCustomLineWidth(float lineWidth);| Name | Type | Default |
|---|---|---|
lineWidth | float | — |
SetDebugPathCustomPointSizevoid
public void SetDebugPathCustomPointSize(float pointSize);| Name | Type | Default |
|---|---|---|
pointSize | float | — |
SetDebugUseCustomvoid
public void SetDebugUseCustom(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetMaxNeighborsvoid
public void SetMaxNeighbors(int maxNeighbors);| Name | Type | Default |
|---|---|---|
maxNeighbors | int | — |
SetMaxSpeedvoid
public void SetMaxSpeed(float maxSpeed);| Name | Type | Default |
|---|---|---|
maxSpeed | float | — |
SetNavigationLayerValuevoid
public void SetNavigationLayerValue(int layerNumber, bool value);Based on value, enables or disables the specified layer in the NavigationAgent2D.NavigationLayers bitmask, given a layer_number between 1 and 32.
| Name | Type | Default |
|---|---|---|
layerNumber | int | — |
value | bool | — |
SetNavigationLayersvoid
public void SetNavigationLayers(uint navigationLayers);Mask of surfaces and links for the agent route. Any shared bit admits a region; this is not an all-keys condition. Changing it with an active goal requests a new path, as checked with 1 → 0 → 1.
| Name | Type | Default |
|---|---|---|
navigationLayers | uint | — |
navigationLayersuint mask. Public rooms on layer 1 and a staff passage on layer 2 require mask 3; the passage bit alone does not admit the rooms.
SetNavigationMapvoid
public void SetNavigationMap(Rid navigationMap);Assigns the agent Entity query map and submits a map change for its server agent. With an active goal, resets the current result for a new search. This is not geometry-only switching: account for map-isolated avoidance.
| Name | Type | Default |
|---|---|---|
navigationMap | Rid | — |
navigationMapRID of a live map for the intended navigation type. Wait for a server update after switching; do not use a freed map.
SetNeighborDistancevoid
public void SetNeighborDistance(float neighborDistance);| Name | Type | Default |
|---|---|---|
neighborDistance | float | — |
SetPathDesiredDistancevoid
public void SetPathDesiredDistance(float desiredDistance);| Name | Type | Default |
|---|---|---|
desiredDistance | float | — |
SetPathMaxDistancevoid
public void SetPathMaxDistance(float maxSpeed);| Name | Type | Default |
|---|---|---|
maxSpeed | float | — |
SetPathMetadataFlagsvoid
public void SetPathMetadataFlags(NavigationPathQueryParameters2D.PathMetadataFlags flags);| Name | Type | Default |
|---|---|---|
flags | NavigationPathQueryParameters2D.PathMetadataFlags | — |
SetPathPostprocessingvoid
public void SetPathPostprocessing(NavigationPathQueryParameters2D.PathPostProcessing pathPostprocessing);| Name | Type | Default |
|---|---|---|
pathPostprocessing | NavigationPathQueryParameters2D.PathPostProcessing | — |
SetPathReturnMaxLengthvoid
public void SetPathReturnMaxLength(float length);| Name | Type | Default |
|---|---|---|
length | float | — |
SetPathReturnMaxRadiusvoid
public void SetPathReturnMaxRadius(float radius);| Name | Type | Default |
|---|---|---|
radius | float | — |
SetPathSearchMaxDistancevoid
public void SetPathSearchMaxDistance(float distance);| Name | Type | Default |
|---|---|---|
distance | float | — |
SetPathSearchMaxPolygonsvoid
public void SetPathSearchMaxPolygons(int maxPolygons);| Name | Type | Default |
|---|---|---|
maxPolygons | int | — |
SetPathfindingAlgorithmvoid
public void SetPathfindingAlgorithm(NavigationPathQueryParameters2D.PathfindingAlgorithmEnum pathfindingAlgorithm);| Name | Type | Default |
|---|---|---|
pathfindingAlgorithm | NavigationPathQueryParameters2D.PathfindingAlgorithmEnum | — |
SetRadiusvoid
public void SetRadius(float radius);| Name | Type | Default |
|---|---|---|
radius | float | — |
SetSimplifyEpsilonvoid
public void SetSimplifyEpsilon(float epsilon);| Name | Type | Default |
|---|---|---|
epsilon | float | — |
SetSimplifyPathvoid
public void SetSimplifyPath(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetTargetDesiredDistancevoid
public void SetTargetDesiredDistance(float desiredDistance);| Name | Type | Default |
|---|---|---|
desiredDistance | float | — |
SetTargetPositionvoid
public void SetTargetPosition(Vector2 position);| Name | Type | Default |
|---|---|---|
position | Vector2 | — |
SetTimeHorizonAgentsvoid
public void SetTimeHorizonAgents(float timeHorizon);| Name | Type | Default |
|---|---|---|
timeHorizon | float | — |
SetTimeHorizonObstaclesvoid
public void SetTimeHorizonObstacles(float timeHorizon);| Name | Type | Default |
|---|---|---|
timeHorizon | float | — |
SetVelocityvoid
public void SetVelocity(Vector2 velocity);| Name | Type | Default |
|---|---|---|
velocity | Vector2 | — |
SetVelocityForcedvoid
public void SetVelocityForced(Vector2 velocity);Replaces the internal velocity in the collision avoidance simulation with NavigationAgent2D.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.
| Name | Type | Default |
|---|---|---|
velocity | Vector2 | — |
Signals and events6
LinkReachedEvent
public event NavigationAgent2D.LinkReachedEventHandler LinkReached;Signals that the agent reached a navigation link. Emitted when the agent moves within NavigationAgent2D.PathDesiredDistance of the next position of the path when that position is a navigation link.
The details dictionary may contain the following keys depending on the value of NavigationAgent2D.PathMetadataFlags:
position: The start position of the link that was reached.type: AlwaysNavigationPathQueryResult2D.PathSegmentType.Link.rid: TheRidof the link.owner: The object which manages the link (usuallyNavigationLink2D).link_entry_position: Ifowneris available and the owner is aNavigationLink2D, it will contain the global position of the link's point the agent is entering.link_exit_position: Ifowneris available and the owner is aNavigationLink2D, it will contain the global position of the link's point which the agent is exiting.
| Name | Type | Default |
|---|---|---|
details | Reckfell.Collections.Dictionary | — |
NavigationFinishedEvent
public event Action NavigationFinished;Signals that the agent's navigation has finished. If the target is reachable, navigation ends when the target is reached. If the target is unreachable, navigation ends when the last waypoint of the path is reached. This signal is emitted only once per loaded path.
This signal will be emitted just after NavigationAgent2D.TargetReached when the target is reachable.
PathChangedEvent
public event 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
NavigationAgent2D.PathMaxDistance.
TargetReachedEvent
public event Action TargetReached;Signals that the agent reached the target, i.e. the agent moved within NavigationAgent2D.TargetDesiredDistance of the NavigationAgent2D.TargetPosition. This signal is emitted only once per loaded path.
This signal will be emitted just before NavigationAgent2D.NavigationFinished when the target is reachable.
It may not always be possible to reach the target but it should always be possible to reach the final position. See NavigationAgent2D.GetFinalPosition.
VelocityComputedEvent
public event NavigationAgent2D.VelocityComputedEventHandler VelocityComputed;Notifies when the collision avoidance velocity is calculated. Emitted every update as long as NavigationAgent2D.AvoidanceEnabled is true and the agent has a navigation map.
| Name | Type | Default |
|---|---|---|
safeVelocity | Vector2 | — |
WaypointReachedEvent
public event NavigationAgent2D.WaypointReachedEventHandler WaypointReached;Signals that the agent reached a waypoint. Emitted when the agent moves within NavigationAgent2D.PathDesiredDistance of the next position of the path.
The details dictionary may contain the following keys depending on the value of NavigationAgent2D.PathMetadataFlags:
position: The position of the waypoint that was reached.type: The type of navigation primitive (region or link) that contains this waypoint.rid: TheRidof the containing navigation primitive (region or link).owner: The object which manages the containing navigation primitive (region or link).
| Name | Type | Default |
|---|---|---|
details | Reckfell.Collections.Dictionary | — |