Get Closest Object - OuterRimStudios/Utilities GitHub Wiki

TargetUtilities.GetClosestObject

public static Transform GetClosestObject(GameObject[] objects, Vector3 originPos)

Parameters

objects Array of objects to compare distances.

originPos Position to check distance from.

Returns

Transform of closest object.

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 closestEnemy = TargetUtilities.GetClosestObject(enemies, transform.position);    //Returns the enemy that is closest to this object
    }
}

public static Transform GetClosestObject(Transform[] objects, Vector3 originPos)

Parameters

objects Array of objects to compare distances.

originPos Position to check distance from.

Returns

Transform of closest object.

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 closestEnemy = TargetUtilities.GetClosestObject(enemies, transform.position);    //Returns the enemy that is closest to this object
    }
}

public static Transform GetClosestObject(List<GameObject> objects, Vector3 originPos)

Parameters

objects List of objects to compare distances.

originPos Position to check distance from.

Returns

Transform of closest object.

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()
    {
        Transform closestEnemy = TargetUtilities.GetClosestObject(enemies, transform.position);    //Returns the enemy that is closest to this object
    }
}

public static Transform GetClosestObject(List<Transform> objects, Vector3 originPos)

Parameters

objects List of objects to compare distances.

originPos Position to check distance from.

Returns

Transform of closest object.

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()
    {
        Transform closestEnemy = TargetUtilities.GetClosestObject(enemies, transform.position);    //Returns the enemy that is closest to this object
    }
}
⚠️ **GitHub.com Fallback** ⚠️