Last active
August 29, 2015 14:22
-
-
Save Henry-E/f138a3680a89001f4dcb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //All this should be in the GameManager Script | |
| // initialise the list | |
| public List<Transform> asteroids; | |
| //this function will all you to use the ship's transform and the asteroid list to find the closest asteroid | |
| void SortCharactersByDistance(Transform myTransform, List<Transform> characterList) | |
| { | |
| characterList.Sort (delegate(Transform t1, Transform t2){ | |
| return Vector3.Distance(t1.position, myTransform.position).CompareTo( | |
| Vector3.Distance (t2.position, myTransform.position)); | |
| }); | |
| } | |
| // this function will be called in the ship's update function to find the closest asteroid | |
| public Transform ClosestAsteroid(Transform myTransform) | |
| { | |
| bool isEmpty = !asteroids.Any(); | |
| if(!isEmpty) | |
| { | |
| SortCharactersByDistance (myTransform, asteroids); | |
| return asteroids[0]; | |
| } | |
| } | |
| // Then in the ship's update function you call | |
| // closestAsteroid = GameManager.instance.ClosestAsteroid(transform); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment