I want to find automatically enemies and put a target on the closest of them. Unity 3D

I would like to put a target on the location of : closest.

p.s. I'm new at this.

public class ExampleClass : MonoBehaviour
{
    public GameObject FindClosestEnemy()
    {
        GameObject[] gos;
        gos = GameObject.FindGameObjectsWithTag("Enemy");
        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
        foreach (GameObject go in gos)
        {
            Vector3 diff = go.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = go;
                distance = curDistance;
            }
        }    //can i put for exemple this specific gameobjet (target)
             //on the closest enemyobject
             //target.transform.position = closest.transform.position;
        return closest;
     }
}

Making a turret follow a target i figured, but automatically find a closest target i just don't understand yet

Upvotes: 0

Views: 4782

Answers (2)

I came with this solution from a tutorial on youtube. at first, i didn't understand the list. I thought that List GameObject was a object and not a type. etc. etc. the video https://www.youtube.com/watch?v=6rDlfYC4HxM He had a method that i actually didn't have "(FindClosest)". That's why i came here because i didn't understand this tutorial and the unity tutorial that i showed you before. public class TARGETER_SCAN : MonoBehaviour { public string detected_TAG1; public string detected_TAG2; public List<GameObject> Enemies_list = new List<GameObject>(); public List<GameObject> TARGETER_LIST = new List<GameObject>(); } void Start() { foreach (GameObject Enemies in GameObject.FindGameObjectsWithTag(detected_TAG1)) { Enemies_list.Add(Enemies); }
foreach (GameObject TARGETER in GameObject.FindGameObjectsWithTag(detected_TAG2)) { TARGETER_LIST.Add(TARGETER); } } void Update() { foreach (var TARGETER in TARGETER_LIST) { var nearest = float.MaxValue; GameObject NearestEnemie = null; foreach (var Enemies in Enemies_list) {
if (Vector3.Distance(transform.position, enemies.transform.position) < nearest) { nearest = (Vector3.Distance(transform.position,Enemies.transform.position)); NearestEnemie = Enemies; target.transform.position = NearestEnemie.transform.position; } } }
What are your thoughts. I don't know if i copied it properly here. I made it work this morning on my project. And it's functional.

Upvotes: 0

derHugo
derHugo

Reputation: 90659

You could simply use Linq OrderBy and First to get the closest object

using System.Linq;
...

public GameObject FindClosestEnemy()
{
    GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");

    // If no enemies found at all directly return nothing
    // This happens if there simply is no object tagged "Enemy" in the scene
    if(enemies.Length == 0)
    {
        Debug.LogWarning("No enemies found!", this);
        return null;
    }

    GameObject closest;

    // If there is only exactly one anyway skip the rest and return it directly
    if(enemies.Length == 1)
    {
        closest = enemies[0];
        target.transform.position = closest.transform.position;
        return closest;
    }


    // Otherwise: Take the enemies
    closest = enemies
    // Order them by distance (ascending) => smallest distance is first element
        .OrderBy(go => (position - go.transform.position).sqrMagnitude)
    // Get the first element
        .First();

    target.transform.position = closest.transform.position;

    return closest;
 }

Upvotes: 1

Related Questions