Get Closest Objects - OuterRimStudios/Utilities GitHub Wiki

TargetUtilities.GetClosestObjects

public static List<Transform> GetClosestObjects(List<GameObject> objects, Vector3 originPos, int amount)

Parameters

objects List of objects to compare distances.

originPos Position to check distance from.

amount Number of objects to return.

Returns

List<Transform> of closest objects.

Description

Calculates the closest object from a given list of GameObjects.

using UnityEngine;
using OuterRimStudios.Utilities;

public class ExampleClass : MonoBehaviour
{
    public List<GameObject> enemies = new List<GameObject>();    //Assuming this is filled in the inspector

    void Start()
    {
        List<Transform> closestEnemies = TargetUtilities.GetClosestObjects(enemies, transform.position, 5);    //Returns the five closest enemies to this object
    }
}

public static List<Transform> GetClosestObjects(List<Transform> objects, Vector3 originPos, int amount)

Parameters

objects List of objects to compare distances.

originPos Position to check distance from.

amount Number of objects to return.

Returns

List<Transform> of closest objects.

Description

Calculates the closest object from a given list of Transforms.

using UnityEngine;
using OuterRimStudios.Utilities;

public class ExampleClass : MonoBehaviour
{
    public List<Transform> enemies = new List<Transform>();    //Assuming this is filled in the inspector

    void Start()
    {
        List<Transform> closestEnemies = TargetUtilities.GetClosestObjects(enemies, transform.position, 5);    //Returns the five closest enemies to this object
    }
}

public static Transform[] GetClosestObjects(GameObject[] objects, Vector3 originPos, int amount)

Parameters

objects Array of objects to compare distances.

originPos Position to check distance from.

amount Number of objects to return.

Returns

Transform[] of closest objects.

Description

Calculates the closest object from a given array of GameObjects.

using UnityEngine;
using OuterRimStudios.Utilities;

public class ExampleClass : MonoBehaviour
{
    public GameObject[] enemies = new GameObject[];    //Assuming this is filled in the inspector

    void Start()
    {
        Transform[] closestEnemies = TargetUtilities.GetClosestObjects(enemies, transform.position, 5);    //Returns the five closest enemies to this object
    }
}

public static Transform[] GetClosestObjects(Transform[] objects, Vector3 originPos, int amount)

Parameters

objects Array of objects to compare distances.

originPos Position to check distance from.

amount Number of objects to return.

Returns

Transform[] of closest objects.

Description

Calculates the closest object from a given array of Transforms.

using UnityEngine;
using OuterRimStudios.Utilities;

public class ExampleClass : MonoBehaviour
{
    public Transform[] enemies = new Transform[];    //Assuming this is filled in the inspector

    void Start()
    {
        Transform[] closestEnemies = TargetUtilities.GetClosestObjects(enemies, transform.position, 5);    //Returns the five closest enemies to this object
    }
}
⚠️ **GitHub.com Fallback** ⚠️