Search Unity

Make enemy follow multiple targets

Discussion in 'Navigation' started by unity_Lsm4Ng1gjLq_7A, Nov 9, 2020.

  1. unity_Lsm4Ng1gjLq_7A

    unity_Lsm4Ng1gjLq_7A

    Joined:
    Nov 3, 2020
    Posts:
    11
    Hello! I'm making an FPS where you have to defend a group of moving objects from enemies. I would like to make the enemies follow whichever target is closest, and have an infinite range to seek them out. How could I implement this, and would this video work?



    Ps: I'm a beginner to C# and Unity.

    Thanks!
     
  2. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    I am not an expert at Unity but what I did to make sure enemies follow multiple targets (in my case, players) was to simply make the Player reference in the script a list instead of a single Transform variable. In your case, an array might be more adequate.

    An array is simply a variable with multiple other variables inside it, called elements. The elements range from 0 to the size limit of the array minus 1, which is set inside the Inspector. So an array with size 5 has elements 0, 1, 2, 3 and 4.

    You set it up like a normal variable but with a [] in front of the variable type, like this:
    public Transform[] Players
    , then to use the individual variables you just use the array name and the element's number, like:
    Player[0] = Something;


    A list is the same as an array but slightly more complex and more versatile. Use it if you need it to add/remove elements from the array automatically in the script. You set it up like this:
    public List<Transform> Players = new List<Transform>();
    , I don't remember exactly why it's redundant like that but still. After setting you can use just like you would with an array but with a few new properties. I believe they aren't as performance friendly as arrays though.

    To make the enemy script detect and follow the closest target you can put a
    for
    statement inside
    void Update ()
    that cycles through all targets in an array and checks the distance for each one, if it's closer then it sets it as their destination:

    Code (CSharp):
    1. public NavMeshAgent Enemy;
    2. public Transform[] Target;
    3.  
    4. void Start ()
    5. {
    6.  
    7. }
    8.  
    9. void Update ()
    10. {
    11.     for (int i = 0; i < Target.size; i++)
    12.     //Target.size returns the size of the array//
    13.     {
    14.         if (Target[i] != null)
    15.         //Makes sure it's following a living target, I recommend creating a boolean inside the target to check if it's dead or not and referecing it here//
    16.         {
    17.             float DistanceFromTarget = Vector3.Distance (Target[i].position, transform.position);
    18.  
    19.             if (i > 0)
    20.             //Never let a script try to grab info from a null element from an array/list, as this creates an error. This makes sure it doesn't take information from Target[-1]//
    21.             {
    22.                 float DistanceFromLastTarget = Vector3.Distance (Target[i - 1].position, transform.position);
    23.             }
    24.             else
    25.             {
    26.                 float DistanceFromLastTarget = 0f;
    27.             }
    28.  
    29.             if (DistanceFromTarget > DistanceFromLastTarget)
    30.             {
    31.                 int MainTarget = i;
    32.             }
    33.         }
    34.     }
    35.  
    36.     Enemy.SetDestination (Target[MainTarget].position);
    37. }
    This might not be the best option but is the most simple one as far as I am aware. I hope I helped!
     
  3. unity_Lsm4Ng1gjLq_7A

    unity_Lsm4Ng1gjLq_7A

    Joined:
    Nov 3, 2020
    Posts:
    11
    Thank you so much !!!!!!!! :)
     
    MinhocaNice likes this.