/// <summary> /// Check for the closest ore vein to mine ore from. /// </summary> /// <param name="agent">The agent we are checking for.</param> /// <returns>If an ore vein was found.</returns> public override bool CheckProceduralPrecondition(GameObject agent) { // Get all the ore veins in the scene. GameObject[] veins = GameObject.FindGameObjectsWithTag("Ore"); GameObject closest = null; float closestDistance = float.MaxValue; // Find the closest ore vein. foreach (GameObject v in veins) { // Check that no one else is targeting this ore vein. OreVein ore = v.GetComponent <OreVein>(); if (ore.GetCurrentMiner() == null) { float dist = (v.transform.position - agent.transform.position).magnitude; // Target the closest ore vein that no one else is targeting. if (dist < closestDistance) { closest = v; closestDistance = dist; } } } if (closest != null) { // Target the closest ore vein. m_Target = closest; // Tell the ore vein that this agent is targeting it. m_Target.GetComponent <OreVein>().SetCurrentMiner(agent.name); return(m_Target != null); } else { return(false); } }