Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Solo Target

Discussion in 'Scripting' started by Leandre5, Jun 28, 2019.

  1. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Hello I'm really new to video game creation and even more so to using Unity

    I would like to create a game with a main character that kills zombies in 2D

    The idea is that the target is automatically chosen (the closest enemy)
    for this reason someone advised me to use the Trasform function of zombies to locate them

    The problem is that I don't know how to aim for just one zombie. Because I imagine that when more than one enemy is going to be in my firing zone it goes to more than one target, but I want it to target only the nearest one.

    Knowing that I don't choose or aim, I always aim at the nearest enemy but I don't see how to sort through the zombies

    https://www.google.com/search?q=arc...AUIESgC&biw=1440&bih=737#imgrc=Frrq6ZdPPKlWtM:


    Here is what I would like to see in the end: several enemy in my firing range but only one who is targeted
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You should head to the Learn section of the website and go through some of the lessons there to get familiar, if you haven't already. Here's a simple way to find the nearest transform from an array of them
    Code (csharp):
    1. public Transform GetClosestEnemy()
    2. {
    3.    Transform[] enemies = GetEnemiesSomeHow(); // I'll assume you have some way of getting all of your enemies. If you don't, consider adding an Enemy script to them and using FindObjectsOfType<Enemy>()
    4.    Transform closest = enemies[0]; // We'll guess that the first enemy is the closest
    5.    for(int i = 1; i < enemies.Length; i++)
    6.    {
    7.        float currentDistance = Vector3.Distance(transform.position, closest.position); // the distance from us to our current closest enemy so far
    8.        float newDistance = Vector3.Distance(transform.position, enemies[i].position); // the distance from us to the enemy we are checking right now
    9.        if(currentDistance > newDistance) // the enemy we are looking at is closer than the last closest one we found
    10.            closest = enemies[i];
    11.    }
    12.    return closest; // this is the enemy with the smallest distance from us to them, so the closest one
    13. }
     
    Thibault-Potier likes this.
  3. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    I would probably hold a list of all the zombies being in your firing zone. Then each frame i would calculate the distance between those zombies and the player, and chose the one with the smaller distance.

    So at the end, your gameObject player would hold a list of zombies. Let's call it zombiesInFireAreaList. Each time a zombie is crossing the fire zone limit (this could be a collider set to isTrigger, allowing you to "detect" new zombie with OnCollisionEnter or something like that) the new zombie would be added to the zombiesInFireAreaList. If for some reason the zombie get out of this area (or if you kill it) you would want to remove him from the zombiesInFireAreaList.

    And know something like that (haven't tested the code) :

    Code (CSharp):
    1. List<GameObject> zombiesInFireAreaList;
    2.  
    3.     float minDistance;
    4.     int closestZombieIndex;
    5.     float currentDist;
    6.  
    7.     private void Update()
    8.     {
    9.         if (zombiesInFireAreaList.Count > 0)
    10.         {
    11.             minDistance = float.MaxValue;
    12.             closestZombieIndex = 0;
    13.  
    14.             for (int i = 0; i < zombiesInFireAreaList.Count; i++)
    15.             {
    16.                 currentDist = Vector3.Distance(transform.position, zombiesInFireAreaList[i].transform.position);
    17.  
    18.                 if (minDistance > currentDist)
    19.                 {
    20.                     minDistance = currentDist;
    21.                     closestZombieIndex = i;
    22.                 }
    23.             }
    24.  
    25.             // here you know you have to shoot the zombie zombiesInFireAreaList[closestZombieIndex]
    26.         }
    27.     }
     
  4. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    thank you very much for your help I will try to put in place what you taught me. A big thank you to you