Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Navigation for Spawned Units, Same Script, 12Different paths.

Discussion in 'Navigation' started by Serkan_D, Sep 3, 2019.

  1. Serkan_D

    Serkan_D

    Joined:
    Jun 13, 2019
    Posts:
    18
    Hi, I am really struggling with this problem, im new into coding and hope somebody can help me out.

    I created a map with 4 Bases, from each base, troops are spawning in 3 directions, and they have to go to all other 3 Bases to attack them, my problem is how can i get my Spawned Units go the Waypoint Path i want? After they destroyed one Base they have to move to the next. Until all enemy Structures are destroyed.
    If Anyone knows WC3 Survival Chaos thats exactly what i am trying to do.

    thanks for any help :)

    Code1.PNG code2.PNG code3.PNG Waypoints.PNG
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    First of all you'll have to make your roads, else the agents will simply beeline towards their target. Place a couple of NavMeshObstacles along the paths to force your agents along them.
    Next, you need to figure out the destination for each unit. This will be your bases. When a unit spawns, give it one of the bases as a destination (using NavmeshAgent.SetDestination(base.transform.position)) and they should walk off immediately.
     
    Serkan_D likes this.
  3. Serkan_D

    Serkan_D

    Joined:
    Jun 13, 2019
    Posts:
    18
    I used an array of Waypoint.Transform to do the pathfinding. It worked but its killing the frames... also they attack one building but dont continue to attack, they try to walk to the next waypoint instead of destroying the building, they attack it once or twice and walk away. I did 2 if statements, is target= true then attack target, is target false then go to the next waypoint. It´s all in the update function...
     
  4. Serkan_D

    Serkan_D

    Joined:
    Jun 13, 2019
    Posts:
    18
    also if it is only one agent, he attacks non stop, but at the moment the second one arrives, the first one walks off to the next waypoint...
     
  5. Serkan_D

    Serkan_D

    Joined:
    Jun 13, 2019
    Posts:
    18
    here is the script
     

    Attached Files:

  6. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    You're continously resetting your destination for your agents each frame, and performing a lot of moderately expensive Distance calculations (following Pythagoras' theorem of a² + b² = c², distance is calculated as sqrt(a² + b²), sqrt is expensive!) to boot.

    You will need to keep their attack target until said target is destroyed and then tell them to move to the next.
    Top of my head you'll want to do something like:
    Code (CSharp):
    1. Transform target;
    2. Building attackTarget;
    3. Agent myAgent;
    4. float attackTreshold;
    5.  
    6. void Start()
    7. {
    8.     myAgent = GetComponent<NavmeshAgent>();
    9. }
    10.  
    11. void Update()
    12. {
    13.     if(agent.remainingDistance <= attackTreshold)
    14.         if(attackTarget != null)
    15.         {
    16.             Attack();
    17.         }
    18.         else
    19.         {
    20.              //Tell the source of your waypoint array to set the next destination with SetTarget
    21.         }
    22. }
    23.  
    24. public void SetTarget(Transform target)
    25. {
    26.     this.target = target;
    27.     if(target is Building)
    28.         attackTarget = (Building)target;
    29.     else
    30.         attackTarget = null;
    31. }
    32.  
    33. void Attack()
    34. {
    35.     //Just keep attacking here, maybe add a timer so you don't attack each frame.
    36. }
     
    Serkan_D likes this.
  7. Serkan_D

    Serkan_D

    Joined:
    Jun 13, 2019
    Posts:
    18
    Thanks for the help, but what does Building mean?
    I searched for it on google but found nothing.
     
  8. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Building would be a class you write yourself to identify buildings. It doesn't need to be called that, you just need to write a class that can be attacked and destroyed (so it can be used for other units as well).
     
  9. Serkan_D

    Serkan_D

    Joined:
    Jun 13, 2019
    Posts:
    18
    Because it opened up a window in unity with Name, Construction Time, finished, construction. I just wondered what i have to fill in.
    But yes ill try to correct my code, thanks.
     
  10. Serkan_D

    Serkan_D

    Joined:
    Jun 13, 2019
    Posts:
    18
    is there a more efficient way to get the Transform of my enemies? i got 3 different enemy tags and 3 different foreach call. And i am just looking for the tags.
     
  11. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Yes, but this is a bit of a workaround (and not really on topic anymore, this kind of question should be asked on the scripting subforum). Looking for anything with Find is not something you want to do frequently, it's a very expensive operation.
    You are going to need a script on each of your enemies. Since your enemies also need a script for their health and such, this can be added in there.
    What this script will do is keep a global list of all enemies on the field. When enemies get added or removed from the field, they will likewise get updated in and out of the list. Then you can use this list to go over all your enemies and do whatever you want.
    Code (CSharp):
    1. public class Enemy : MonoBehavior
    2. {
    3.     static public List<Enemy> allEnemies = new List<Enemy>();
    4.  
    5.     void OnEnable()
    6.     {
    7.         allEnemies.Add(this);
    8.     }
    9.  
    10.     void OnDisable()
    11.     {
    12.         allEnemies.Remove(this);
    13.     }
    14. }
    You can then access this list using Enemy.allEnemies and iterate over it like any other.
    Eventually you might reuse this code for all units on the field and keep separate lists for each team, so enemies can look for the player's units the same ways. A good idea then is to move those lists to Player classes (Player can then both be used for the actual player and the enemy computer) and then tell each unit what team they belong to so they add themselves to the correct list.