Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement