/// Finds the nearest target to the given position. /// Returns null if no such target exists. public GameObject FindNearestTarget(Vector3 to) { float minimumDistance = float.PositiveInfinity; GameObject result = null; // Loop over every ship, checking if this missile is targeting their team. // TODO: Also loop over space-flares! Possibly by marking flares as your team. // TODO: Seperate missiles and ships, so flares can work properly. foreach (GameObject ship in TeamMarker.GetAllMarkedGameObjects()) { CombatTeam team = ship.GetComponent <TeamMarker>().Team; if (IsTargeting(team)) { float distance = Vector3.Distance(ship.transform.position, to); // This ship is closer than the previously closest ship. if (distance < minimumDistance) { result = ship; minimumDistance = distance; } } } return(result); }
/// Helper for determining if a game object is targeted. public bool IsTargeting(GameObject gameObject) { TeamMarker marker = gameObject.GetComponent <TeamMarker>(); if (marker != null) { return(IsTargeting(marker.Team)); } return(false); }