Skip to content

Instantly share code, notes, and snippets.

@rdeioris
Created October 27, 2017 09:33
Show Gist options
  • Save rdeioris/141e1013cee3b467fb29250e2131553f to your computer and use it in GitHub Desktop.
Save rdeioris/141e1013cee3b467fb29250e2131553f to your computer and use it in GitHub Desktop.
using UnityEngine;
public class FindBestMineral : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
public Mineral GetNearestMineral()
{
Mineral bestMineral = null;
float distanceFromBest = float.MaxValue;
Mineral[] minerals = GameObject.FindObjectsOfType<Mineral>();
foreach (Mineral mineral in minerals)
{
if (!mineral.wild)
continue;
float distance = (mineral.transform.position - transform.position).magnitude;
if (distance < distanceFromBest)
{
distanceFromBest = distance;
bestMineral = mineral;
}
}
return bestMineral;
}
// Update is called once per frame
void Update()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment