unity find nearest object

void FindClosest()
    {     
        float distanceToClosestEnemy = Mathf.Infinity;
        Enemy closestEnemy = null;
       //Edit Enemy in the FindObjectsOfType to a component on the object you
       //want to find nearest 
        Enemy[] allEnemies = GameObject.FindObjectsOfType<Enemy>();

        foreach (Enemy currentEnemy in allEnemies)
        {
            float distanceToEnemy = (currentEnemy.transform.position - this.transform.position).sqrMagnitude;
            if (distanceToEnemy < distanceToClosestEnemy)
            {
                distanceToClosestEnemy = distanceToEnemy;
                closestEnemy = currentEnemy;
            }
        }
  
       //If you want to move to nearest object 
       if(allEnemies.Length > 0) 
       {
           Ally.transform.position = Vector2.MoveTowards(Ally.transform.position, closestEnemy.transform.position, speedMoving * Time.deltaTime);

       }
    }

3.71
7
Awgiedawgie 440220 points

                                    Transform GetClosestEnemy(Transform[] enemies)
{
    Transform tMin = null;
    float minDist = Mathf.Infinity;
    Vector3 currentPos = transform.position;
    foreach (Transform t in enemies)
    {
        float dist = Vector3.Distance(t.position, currentPos);
        if (dist &lt; minDist)
        {
            tMin = t;
            minDist = dist;
        }
    }
    return tMin;
}

3.71 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
unity get nearest point from object find nearest unity unity how to find closest object unity find nearest obejct unity how to calculate the nearest object unity how to find a nearest object how to find the nearest object unity unity go to nearest object unity get nearest gameobject find nearest game object unity unity find game object nearest how to find the nearest object with a tag unity find nearest gameobject unity how to find nearest gameobject unity unity search nearest object unity find gameobject nearest how to search for nearest object unity unity nearest of objects get closest object unity make an object look at the nearest object unity unity find closest object unity closes object how to check for nearby enemies unity unity measure which player is closest find closest object from a list unity Unity3D AI how to find the closest object closest object c# unity unity find the nearest object get closer to object unity c# unity get nearest object from vector3[] unity get nearest object with component unity get nearest object find closest object direction unity 2d unity 2d get closest object to poing find closest object unity unity find how close object is find nearest xz from path for third object in unity unity least resources way to get closest object unity fastest way to get closest object unity c# find closest object unity c# find nearest object find closest item unity unity 2d find nearest object get nearest object unity how to find closest object unity how to find nearest object unity check for closest object in unity find enemy closest to player unity find the closest object unity how to find the nearest enemy unity 2d unity get closest object unity get data from nearest object Unity find nearest point in list how to get nearby objects in unity Find closest gameobject in a gameobject array unity c\ unity closest object unity find nearest instance fin clostest object unity find closest gameobject unity check close to object get object in list closest to another unity unity find nearest object
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source