Struct NearestNodeConstraint
Public
Constraint for the AstarPath.GetNearest method.
This is used to filter out nodes you are not interested in.
For example, you can use this to only find walkable nodes, or only nodes with a specific tag.
Why is this struct passed around with the ref keyword all the time?
Structs are passed by value in C#, and copying them can be expensive when they are large (like this one at around 44 bytes). Using the ref keyword allows it to be passed by reference instead, which is much faster.
But then why is it not a class?
Firstly, because classes involve the garbage collector, and since this struct is created very often (often many times per frame), we want to avoid that. Garbage collections are very expensive operations, and can cause stuttering in the game.
Public Methods
Public Variables
bool
allNodesAreSuitable
True if all nodes are suitable with respect to the constraints set in this struct.
The Suitable method will return true for all nodes if this is true.
int
area
Area ID to constrain to.
Will not affect anything if negative.
Determines how to measure distances to the navmesh.
The default is a euclidean distance, which works well for most things.
System.Func<GraphNode, bool>
filter
Allow only nodes which the given filter function returns true for.
The other constraints on this struct will still apply, in addition to the filter function.
var nearestNodeConstraint = NearestNodeConstraint.None;
// Set a custom filter. This can be an arbitrary function which takes a GraphNode and returns true or false.
nearestNodeConstraint.filter = (GraphNode node) => ((Vector3)node.position).y > 50;
// Find the closest node to our position which is above y=50
var nearest = AstarPath.active.GetNearest(transform.position, nearestNodeConstraint);
Graphs which are traversable.
This is a bitmask, meaning that bit 0 specifies whether or not the first graph in the graphs list should be included in the search, bit 1 specifies whether or not the second graph should be included, and so on.
var traversalConstraint = TraversalConstraint.None;
// Allows the first and fourth graphs to be searched, but not the rest
traversalConstraint.graphMask = GraphMask.FromGraphIndex(0) | GraphMask.FromGraphIndex(3);
// Allows the two named graphs to be searched, but not the rest
traversalConstraint.graphMask = GraphMask.FromGraphName("My Custom Graph Name") | GraphMask.FromGraphName("My Other Graph");
NoteWhen used for pathfinding, this does only affects which nodes the path starts and ends on. If a valid graph is connected to an "invalid" graph, it may get searched anyway.
float???
maxDistance
The maximum distance to the navmesh to search before the search fails.
This is the maximum distance from the query point to the closest point on the navmesh. If null (the default), the default limit set on the AstarPath component will be used ( AstarPath.maxNearestNodeDistance).
Positive infinity is a valid value, meaning it will search for the closest node regardless of distance. 0 is also a valid value, but it usually only makes sense with a different distance metric than the default.
NoteThe distance depends on the distance metric used.
Allow traversing only nodes that are also traversable by the given ITraversalProvider.
The other constraints on this struct will still apply, in addition to the ITraversalProvider.
Constraint to only search only walkable nodes, only unwalkable nodes, or both.
Public Static Variables
Constraint which allows all nodes.
Constraint which filters out all unwalkable nodes.
Public Enums
WalkabilityConstraint
Walkable |
|
Unwalkable |
|
DontCare |
|
Private/Protected Members
Sub-filter with more constraints.