Always find a closest point

Hello.
I have an AI(the small ghost on the picture) and a target for it(the big ghost on the picture). When I destroy a ball on the board I update(scan) available points.
Is it possible to make small ghost always move to the closet available point when I shoot(Scan the points).
I use a AILerp script to move the small ghost.
Thanks in advance!!!

Hi

Moving to the closest point the AI can reach should be the default behavior. It will try to find points up to a distance of A* Inspector -> Settings -> Max Nearest Node Distance. If the distance to the closest point it could reach is greater than that value then the path will fail instead.

Sorry, but I think that Iā€™m doing something wrong and itā€™s not move to the nearest available point.
Could you please check?

Hi

That error is typically logged when the ā€˜Max Nearest Node Distanceā€™ is too low. Try increasing it.

I tried 500 and nothing happendā€¦

I thouht that it should move to black circle for example.
Is it possible?

Thatā€™s odd. Which point are you trying to move to?

the small ghost to the big three ghosts.
It will move only if the path will be without any obstacles to the end. I mean if there will no be any obstacles.

Any idea? What it can be

What do you mean by to the three big ghosts? Thatā€™s 3 points, not one.

red circle.
For example if I remove red balls above the black circle, the small ghost will immediately move to the target

Hmā€¦ I donā€™t know. If you set Max Nearest Node Distance to something like 100, it really should work in this caseā€¦ Iā€™m not sure why it doesnā€™tā€¦

So, what is your suggest?

Do you think you could send me a simple test scene which shows the problem, and then I could try to debug it.
Please specify your Unity version as well.

Yes, I can do it. I will prepare it. I use Unity 2017 2

Okā€¦
Open the ā€œTestPointGraphā€ scene
Start the sceneā€¦
Hierarchy -> Body -> Grid -> TESTBALL
Activate this object and press the ā€œScanā€ in A*.
And only after that the small ghost wil go.
Maybe Iā€™m wrong, but I thought that it should go to the nearest point to the target, for example black circle on the image.

Hi

Thanks for sharing that!
It looks like you have stumbled upon a bug. Specifically this bug only happened in the free version when using a point graph. To fix it, open the PointGenerator.cs script and replace the two methods GetNearest and GetNearestForce with this:

public override NNInfoInternal GetNearest (Vector3 position, NNConstraint constraint, GraphNode hint) {
	return GetNearestInternal(position, constraint, true);
}

public override NNInfoInternal GetNearestForce (Vector3 position, NNConstraint constraint) {
	return GetNearestInternal(position, constraint, false);
}

NNInfoInternal GetNearestInternal (Vector3 position, NNConstraint constraint, bool fastCheck) {
	if (nodes == null) return new NNInfoInternal();

	float maxDistSqr = constraint == null || constraint.constrainDistance ? AstarPath.active.maxNearestNodeDistanceSqr : float.PositiveInfinity;

	var nnInfo = new NNInfoInternal(null);
	float minDist = float.PositiveInfinity;
	float minConstDist = float.PositiveInfinity;

	for (int i = 0; i < nodeCount; i++) {
		PointNode node = nodes[i];
		float dist = (position-(Vector3)node.position).sqrMagnitude;

		if (dist < minDist) {
			minDist = dist;
			nnInfo.node = node;
		}

		if (dist < minConstDist && dist < maxDistSqr && (constraint == null || constraint.Suitable(node))) {
			minConstDist = dist;
			nnInfo.constrainedNode = node;
		}
	}

	if (!fastCheck) nnInfo.node = nnInfo.constrainedNode;

	nnInfo.UpdateInfo();
	return nnInfo;
}

Aron, thanks for your help. I will try this code in the morning and let you know about results.
And again, thank you very much

Aron - it works. Thank you very much.

1 Like