Answers for "unity point towards nearest tag"

C#
0

unity point towards nearest tag

// Find the name of the closest enemyusing UnityEngine;
using System.Collections;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;
            }
        }
        return closest;
    }
}
Posted by: Guest on December-21-2020

Code answers related to "unity point towards nearest tag"

C# Answers by Framework

Browse Popular Code Answers by Language