find closest enemy in sight

public class turretBehaviour : MonoBehaviour {

public float rotationSpeed;
public float activationRange;
public GameObject turretShellSpawnpoint;
public GameObject[] enemies;
public List<GameObject> enemiesVisible;


// Use this for initialization
void Start () {
	enemies= new GameObject[100];
    enemiesVisible = new List<GameObject>();
}

// Update is called once per frame
void Update () {
    EnemyInSight();
    foreach (GameObject enemyVisible in enemiesVisible)
    {
        Debug.DrawLine(turretShellSpawnpoint.transform.position, enemyVisible.transform.position, Color.yellow);
        // Debug.Log("spara!spara!spara!");
    }
    /*   if (Vector3.Distance(FindClosestEnemyInSight(enemiesVisible).transform.position, transform.position) < activationRange)
       {
           SmoothLook(FindClosestEnemyInSight(enemiesVisible).transform.position - transform.position);
       }
     */
    //  SmoothLook(FindClosestEnemyInSight(enemiesVisible).transform.position - transform.position);
    Debug.DrawLine(turretShellSpawnpoint.transform.position, FindClosestEnemyInSight(enemiesVisible).transform.position, Color.red);

    /*if(EnemyInSight()==true)
    {
        Debug.Log("spara!spara!spara!");
    }
    */
    

}

public GameObject FindClosestEnemyInSight(List<GameObject> gos)
{
    
   // GameObject[] gos;
   // gos = GameObject.FindGameObjectsWithTag("Enemy");
    GameObject closest = null;
    float distance = Mathf.Infinity;
    Vector3 position = turretShellSpawnpoint.transform.position;
    foreach (GameObject go in gos)
    {

        Vector3 diff = go.transform.position - position;
        float curDistance = diff.sqrMagnitude;
        if (curDistance < distance)
        {
            closest = go;
            distance = curDistance;
            
        }
    }
    return closest;
}
//da finire--------------------------------
private void /*bool*/ EnemyInSight()
{
    
    enemies = GameObject.FindGameObjectsWithTag("Enemy");
    
    foreach (GameObject enemy in enemies)
    {
        Ray myRay = new Ray(turretShellSpawnpoint.transform.position, enemy.transform.position);
        RaycastHit hitInfo = new RaycastHit();
       // Debug.DrawLine(transform.position, enemy.transform.position);
        Physics.Raycast(myRay, out hitInfo);
        
        if (hitInfo.collider == null)
        {
            // if(enemy== FindClosestEnemy())
            //  {
            if (enemiesVisible.Contains(enemy))
            {                    
                //so già che è in vista quindi non o aggiungo alla lista

            }
            else
            {
                enemiesVisible.Add(enemy);
                
            }
                
            //  }
                
        }
        else
        {
            enemiesVisible.Remove(enemy);
        }
        
        
    }

    //List<RaycastHit> mylist = new List<RaycastHit>();
    //Ray myRay = new Ray(transform.position, FindClosestEnemy().transform.position);
    //Debug.DrawLine(transform.position, FindClosestEnemy().transform.position);
    // RaycastHit hitInfo=new RaycastHit();

    //int hitcounter;
    //Physics.Raycast(transform.position, FindClosestEnemy().transform.position-transform.position,hitInfo, activationRange);
    /*
    if (Physics.Raycast(myRay,out hitInfo))
    {
        if (hitInfo.collider != null && hitInfo.collider.gameObject.tag=="Enemy")
        {
            Debug.DrawLine(transform.position, FindClosestEnemy().transform.position);
            Debug.Log("spara!spara!spara!");
        }
        

    }
    */

    /*
    if (hitInfo.collider.gameObject== FindClosestEnemy())
    {
        //return true;

        Debug.Log("spara!spara!spara!");
    }
    else
    {
       // return false;
    }
    */
    
}

void SmoothLook(Vector3 newDirection)
{
    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(newDirection), rotationSpeed);
}

}

Use SphereCastAll to get all the objects within a distance. The implementation below assumes that your Ememies have the Layer of ‘Enemy’, if you don’t have it you need to create a layer and assign your enemy to that layer.

public string enemyLayerName = "Enemy";

Transform GetClosestEnemy () {
	// Get the Ray
	LayerMask enemyLayer = 1 << LayerMask.NameToLayer (enemyLayerName);
	
	// Create a ray
	Ray ray = new Ray(transform.position, transform.forward);
	
	// Set the Distance
	float distance = 0; // We don't want it to be in a line. Simply around the player
	
	// Set the Radius
	float radius = 100; // 100m around the player
	
	// Choose to if you want to include triggers or not
	QueryTriggerInteraction triggerInteraction = QueryTriggerInteraction.Ignore; // Other options are Collide and UseGlobal - Use Global is the default
	
	// Do the cast
	RaycastHit[] hits = SphereCastAll(ray, radius, distance, enemyLayer, QueryTriggerInteraction.UseGlobal);
	
	/* Now that we have all the enemies find the closest */ 
	if (hits.Length == 0)
		return null;
		
	Transform closestEnemy = hits[0].transform;
	Vector3 closestEnemyDist = Vector3.Distance(transform.position, hits[0].transform.position) ;
	
	for (int i = 1; i < hits.Length; i++){
		var _enemy = hits*.transform;		*
  •  var _dist = Vector3.Distance(transform.position, _enemy.position);*
    
  •  if (_dist < closestEnemyDist){*
    
  •  	closesEnemy = _enemy;*
    
  •  	closestEnemyDist = _dist;*
    
  •  }*
    
  • }*

  • // Finally return closest*

  • return closestEnemy; *
    }