Class NavigationAgent3D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
A 3D 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 NavigationAgent3D : Node, IDisposable
- Inheritance
-
NavigationAgent3D
- Implements
- Inherited Members
Constructors
NavigationAgent3D()
public NavigationAgent3D()
Properties
AvoidanceEnabled
If true the agent is registered for an RVO avoidance callback on the NavigationServer3D. When Velocity is set and the processing is completed a safe_velocity
Vector3 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
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
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
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
DebugEnabled
If true shows debug visuals for this agent.
public bool DebugEnabled { get; set; }
Property Value
DebugPathCustomColor
If DebugUseCustom is true uses this color for this agent instead of global color.
public Color DebugPathCustomColor { get; set; }
Property Value
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
DebugUseCustom
If true uses the defined DebugPathCustomColor for this agent instead of global color.
public bool DebugUseCustom { get; set; }
Property Value
Height
The height of the avoidance agent. Agents will ignore other agents or obstacles that are above or below their current position + height in 2D avoidance. Does nothing in 3D avoidance which uses radius spheres alone.
public float Height { get; set; }
Property Value
KeepYVelocity
If true, and the agent uses 2D avoidance, it will remember the set y-axis velocity and reapply it after the avoidance step. While 2D avoidance has no y-axis and simulates on a flat plane this setting can help to soften the most obvious clipping on uneven 3D geometry.
public bool KeepYVelocity { get; set; }
Property Value
MaxNeighbors
The maximum number of neighbors for the agent to consider.
public int MaxNeighbors { get; set; }
Property Value
MaxSpeed
The maximum speed that an agent can move.
public float MaxSpeed { get; set; }
Property Value
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
NeighborDistance
The distance to search for other agents.
public float NeighborDistance { get; set; }
Property Value
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 it 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 the distance to the next point on each physics frame update.
public float PathDesiredDistance { get; set; }
Property Value
PathHeightOffset
The height offset is subtracted from the y-axis value of any vector path position for this NavigationAgent. The NavigationAgent height offset does not change or influence the navigation mesh or pathfinding query result. Additional navigation maps that use regions with navigation meshes that the developer baked with appropriate agent radius or height values are required to support different-sized agents.
public float PathHeightOffset { get; set; }
Property Value
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
PathMetadataFlags
Additional information to return with the navigation path.
public NavigationPathQueryParameters3D.PathMetadataFlags PathMetadataFlags { get; set; }
Property Value
PathPostprocessing
The path postprocessing applied to the raw path corridor found by the PathfindingAlgorithm.
public NavigationPathQueryParameters3D.PathPostProcessing PathPostprocessing { get; set; }
Property Value
PathfindingAlgorithm
The pathfinding algorithm used in the path query.
public NavigationPathQueryParameters3D.PathfindingAlgorithmEnum PathfindingAlgorithm { get; set; }
Property Value
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
SimplifyEpsilon
The path simplification amount in worlds units.
public float SimplifyEpsilon { get; set; }
Property Value
SimplifyPath
If true a simplified version of the path will be returned with less critical path points removed. The simplification amount is controlled by 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".
public bool SimplifyPath { get; set; }
Property Value
TargetDesiredDistance
The distance threshold before the target is considered to be reached. On reaching the target, TargetReached is emitted and navigation ends (see IsNavigationFinished() and NavigationFinished).
You can make navigation end early by setting this property to a value greater than PathDesiredDistance (navigation will end before reaching the last waypoint).
You can also make navigation end closer to the target than each individual path position by setting this property to a value lower than PathDesiredDistance (navigation won't immediately end when reaching the last waypoint). However, if the value set is too low, the agent will be stuck in a repath loop because it will constantly overshoot the distance to the target on each physics frame update.
public float TargetDesiredDistance { get; set; }
Property Value
TargetPosition
If set, a new navigation path from the current agent position to the TargetPosition is requested from the NavigationServer.
public Vector3 TargetPosition { get; set; }
Property Value
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
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
Use3DAvoidance
If true, the agent calculates avoidance velocities in 3D omnidirectionally, e.g. for games that take place in air, underwater or space. Agents using 3D avoidance only avoid other agents using 3D avoidance, and react to radius-based avoidance obstacles. They ignore any vertex-based obstacles.
If false, the agent calculates avoidance velocities in 2D along the x and z-axes, ignoring the y-axis. Agents using 2D avoidance only avoid other agents using 2D avoidance, and react to radius-based avoidance obstacles or vertex-based avoidance obstacles. Other agents using 2D avoidance that are below or above their current position including Height are ignored.
public bool Use3DAvoidance { get; set; }
Property Value
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(Vector3) as well to reset the internal simulation velocity.
public Vector3 Velocity { get; set; }
Property Value
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
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
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
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 Vector3[] GetCurrentNavigationPath()
Returns
- Vector3[]
GetCurrentNavigationPathIndex()
Returns which index the agent is currently on in the navigation path's Vector3[].
public int GetCurrentNavigationPathIndex()
Returns
GetCurrentNavigationResult()
Returns the path query result for the path the agent is currently following.
public NavigationPathQueryResult3D GetCurrentNavigationResult()
Returns
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 Vector3 GetFinalPosition()
Returns
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
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
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 Vector3 GetNextPathPosition()
Returns
GetRid()
Returns the Rid of this agent on the NavigationServer3D.
public Rid GetRid()
Returns
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_nameName of the method to check for.
Returns
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_nameName of the signal to check for.
Returns
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_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsNavigationFinished()
Returns true if 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.
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
IsTargetReachable()
Returns true if GetFinalPosition() is within TargetDesiredDistance of the TargetPosition.
public bool IsTargetReachable()
Returns
IsTargetReached()
Returns true if the agent reached the target, i.e. the agent moved within TargetDesiredDistance of the TargetPosition. It may not always be possible to reach the target but it should always be possible to reach the final position. See GetFinalPosition().
public bool IsTargetReached()
Returns
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
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
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
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(Vector3)
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(Vector3 velocity)
Parameters
velocity
Vector3
Events
LinkReached
Signals that the agent reached a navigation link. Emitted when the agent moves within 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 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 NavigationLink3D).
- link_entry_position
: If owner
is available and the owner is a NavigationLink3D, 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 NavigationLink3D, it will contain the global position of the link's point which the agent is exiting.
public event NavigationAgent3D.LinkReachedEventHandler LinkReached
Event Type
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 TargetReached when the target is reachable.
public event Action NavigationFinished
Event Type
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
TargetReached
Signals that the agent reached the target, i.e. the agent moved within TargetDesiredDistance of the TargetPosition. This signal is emitted only once per loaded path.
This signal will be emitted just before 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 GetFinalPosition().
public event Action TargetReached
Event Type
VelocityComputed
Notifies when the collision avoidance velocity is calculated. Emitted every update as long as AvoidanceEnabled is true and the agent has a navigation map.
public event NavigationAgent3D.VelocityComputedEventHandler VelocityComputed
Event Type
WaypointReached
Signals that the agent reached a waypoint. Emitted when the agent moves within PathDesiredDistance of the next position of the path.
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 NavigationAgent3D.WaypointReachedEventHandler WaypointReached