- Home /
How to compare several distances?
Hey, thank you for reading through here!
So, I try to say it as short as possible; I've made good progress in my 2D Grand Strategy game, but don't know how I should make the AI. Well, I technically know what I want and how it would work, but I don't know how it is coded. Basically, a Physics2D.OverlapCircle is detecting which provinces are around an unit for the unit to find it's path to the target province which is in the contested zone.
I thought that for the pathfinding the unit will see which provinces are around it and then compare the distance of each of them to the target province. It moves to the one with the shortest distance then and repeats this process every turn to eventually arrive in the target province.
Now, how can I get the distances for for example four provinces to the target province and compare each of the distances to get the smallest one? Or, asked differently, how can I use Vector2.Distance for several inputs and seperate them to compare them and find the shortest one?
Code for getting the object inside the circle:
Collider2D[] borderingProvinces = Physics2D.OverlapCircleAll(transform.position, 10);
foreach (Collider2D hit in borderingProvinces)
{
if (hit.tag == "Province")
{
// here comparing to other provinces!
}
else if (hit.tag == "playerProvince")
{
Debug.Log("Player's Province spotted!");
transform.position = hit.transform.position;
}
else
{
Debug.Log("Error!");
}
}
provide the code for getting the objects that are inside the sphere so we can help you :)
Answer by TreyH · Nov 04, 2019 at 03:23 PM
The basic solution for this problem will be something like:
Transform FindClosest()
{
// Code for getting the object inside the circle:
Collider2D[] borderingProvinces = Physics2D.OverlapCircleAll(transform.position, 10);
// Start with an impossibly high distance for comparison
float closestDistance = 99999;
Transform closest = null;
foreach (Collider2D hit in borderingProvinces)
{
if (hit.tag == "Province")
{
var distance = (transform.position - hit.transform.position).magnitude;
if (distance < closestDistance)
{
closest = hit.transform;
closestDistance = distance;
}
}
else if (hit.tag == "playerProvince")
{
Debug.Log("Player's Province spotted!");
transform.position = hit.transform.position;
}
}
return closest;
}
You can simplify this a bit with something like Linq, though. I'm not counting this as an answer as I don't really understand the question. Are these distances really the only thing determining your pathfinding? Even though this does get the "closest" of your provinces, that might be irrelevant from a full path you're trying to derive.
I want the shortest distances of all provinces to the target province that are within the Physics2D.OverlapCircle. I don't know how to simplify this more ;(
$$anonymous$$aybe this helps you to understand me: https://forum.unity.com/threads/ai-province-navigation-in-strategy-game.771119/
I'm not good at such drawings but this might as well: https://imgur.com/a/uon4Bvv
but Vector3.Distance already returns the shortest distance from object A to object B, if you want all the distances you just iterate over all of them and save them, whats exactly giving you issues? why is treyh comment not working for you?
I might have not been very clear, I apologize. I wanted to keep the question as short as possible. You can look into the Forum link if you want though. So basically, a unit can move once per turn, and the AI always wants to bring them to the frontline. But when there are several provinces seperating the unit from it's destination, the AI has to find a path. Grids and similar won't work for a turn-based province-to-province system, that's why the Physics2D.OverlapCircleAll has to detect which province in it's range is the nearest to the destination and then moves there. Like this, the AI should be able to bring units to the frontline even if 20 provinces are seperating them.
Your answer
Follow this Question
Related Questions
How to use 2D pathfinding with vector 2 0 Answers
Distribute terrain in zones 3 Answers
Extend a line between two Vectors by a distance 0 Answers
Find alternate shortest paths 0 Answers
A* pathfinding for 2D top Down 0 Answers