/* * Get the nearest target within a cone of vision. * If there are no hazards within the COV, return null. */ Transform FindTarget() { ArrayList hazards = gameController.GetHazards(); //If there are no current hazards, return null. if (hazards.Count == 0) return null; GameObject closest = null; //Get closest hazard to lock onto foreach (GameObject item in hazards) { if (item != null && item.tag != "Powerup") { //Check COV Vector3 direction = item.transform.position - transform.position; if (Vector3.Angle(transform.forward, direction) <= visionAngle) { //Replace 'closest' if null or 'item' is closer. if (closest == null || (closest.transform.position - transform.position).sqrMagnitude >= (item.transform.position - transform.position).sqrMagnitude) { closest = item; } } } } if (closest == null) return null; return closest.transform; }