I have ranged units in one region that can potentially shoot targets in another region, if they were to get closer. How would I get the closest point to the target that the ranged unit can navigate to?
Hey,
You can customize the seeker to find the closest node (Though this is the default )
Though I’m not sure how that works for the destination being on a different graph.
To manually calculate the closest point on the current graph you could use the GetNearest( Vector3, NNConstraint) function. Where you set the current graph of your agent as SuitableGraph
I don’t want to move to the closest point, just to get the closest point that I can walk to.
I think it’s this?
public Vector3 GetNearestReachableToPoint(Vector3 point)
{
NNInfo startNode = AstarPath.active.GetNearest(transform.position, NNConstraint.Default);
nnc.constrainArea = true;
nnc.area = (int) startNode.node.Area;
nnc.graphMask = graphMask;
nnc.constrainWalkability = true;
nnc.walkable = true;
NNInfo destinationNode = AstarPath.active.GetNearest(point, nnc);
Vector3 destinationSnapped = destinationNode.position;
// Prevent returning a nearby point on a different level
if (Mathf.Abs(destinationSnapped.y - point.y) > GetStepOffset() && (destinationSnapped - point).sqrMagnitude < 10.0f * 10.0f)
return point;
return destinationSnapped;
}
public Vector3 GetNearestReachableToPoint(Vector3 point)
{
NNInfo startNode = AstarPath.active.GetNearest(transform.position, NNConstraint.Default);
nnc = NNConstraint.Default;
nnc.area = (int) startNode.node.Area;
NNInfo destinationNode = AstarPath.active.GetNearest(point, nnc);
Vector3 destinationSnapped = destinationNode.position;
// Prevent returning a nearby point on a different level
if (Mathf.Abs(destinationSnapped.y - point.y) > GetStepOffset() && (destinationSnapped - point).sqrMagnitude < 10.0f * 10.0f)
return point;
return destinationSnapped;
}
Think you can simplify it a little by just using the Constructor Default
But ye I think that should work
Hi
Just requesting a path to the target should make the agent navigate to the closest point that it can reach (assuming your A* Inspector -> Settings -> Max Nearest Node Distance is high enough).
If you just want to get the closet point without moving to it, then yes: that code should work perfectly.