Created
April 30, 2017 09:13
-
-
Save dsapandora/da11c5617b86a02e3f4a8ceeaecf34a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment