Search Unity

AI Province Navigation in Strategy Game

Discussion in 'Scripting' started by Revifle_Monarch_Studios, Nov 3, 2019.

  1. Revifle_Monarch_Studios

    Revifle_Monarch_Studios

    Joined:
    Apr 22, 2019
    Posts:
    1
    Hello and thank you for reading through this thread!


    A few days ago, I came up with a sort-of small "Grand"-Strategy game, which is based in 1943 on the Allied Invasion onto Italy. I had some good progress but did not invest a thought into the unit-management AI once. Now, I stand before that problem.

    What I want is that all provinces of the AI that border one of the playerProvinces are "contested" and the AI will move it's unit to the contested provinces so they are on the front and not somewhere in the backland, waiting to be attacked.

    A province get's the tag "contested" if it collides with a playerProvince via Physics2D.OverlapCircle. The unit itself has an Physics2D.OverlapCircle as well, which checks for it's neighbouring provinces, the provinces that are in range to move to. An important thing to say as well is that every unit can only move once per turn. So, if the unit is for example four provinces away from it's target province, it has to find a path to there. I'm really not a fan of grids and similiar, thus I "came up" (don't know if it works though) with another way;
    the unit checks it's distance to the target province. If it is out-of-range, it looks into it's Physics2D.OverlapCircle and looks onto the provinces around itself. Now, to find the best "path", it compares all the distances of every province within it's radius to the target province and then moves to the one that is the nearest to the target province. This process is done every turn again until the unit arrives at the target province, as it can only move once per turn. I can imagine this sounding a bit messy and confusing, so I (at least tried) drew some movements in as how I imagine them. Prepare for the worst looking drawing!

    https://imgur.com/a/uon4Bvv


    That said, I'm not even done with explaining myself :) Now, I don't exactly now how to compare the distances for each province. I know how I can do it for one single province, but how to compare the provinces that fall into the Physics2D.OverlapCircle? How to seperate them to do the distance-comparison for every single one? That's the thing where I need help. Oh, and another thing would be helpful to know as well; if I use GameObject.FindWithTag and search for gameObjects of the tag "contestedProvince", is the unit going to pick one of several randomly or is nothing going to happen? For I want the unit to look onto all provinces that are contested and get one of them to become the target province.

    Thanks in advance! I'm looking forward to your answers :)