Skip to content

Instantly share code, notes, and snippets.

@Henry-E
Last active August 29, 2015 14:22
Show Gist options
  • Save Henry-E/f138a3680a89001f4dcb to your computer and use it in GitHub Desktop.
Save Henry-E/f138a3680a89001f4dcb to your computer and use it in GitHub Desktop.
//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