Answers for "make an object look at the nearest object unity"

C#
0

unity find nearest object

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 < minDist)
        {
            tMin = t;
            minDist = dist;
        }
    }
    return tMin;
}
Posted by: Guest on September-30-2020

Code answers related to "make an object look at the nearest object unity"

C# Answers by Framework

Browse Popular Code Answers by Language