Shooting nearest enemy through my turret is buggy

I have a script that makes my turret target the nearest enemy then face the enemy. if the enemy is close enough it will shoot (Not specifically at the enemy but in the direction it is facing which is the enemy). However when it sees another enemy it will somehow shoot both like so99837-capasdasdasd.png

Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class A2Script : MonoBehaviour {

GameObject[] enemies;
public GameObject player;
string Enemytag = "Enemy";
GameObject CurrentTarget;
private Vector3 target;
float count = 1;
public float range = 15;
public Rigidbody projectile;
public GameObject rightGun;
public GameObject leftGun;

//public GameObject turretGun;
//public GameObject guns;

void Start () {

    //InvokeRepeating("MoveTurret", 0f, .1f);	
        }

void Update () {
   // MoveGun();
    MoveTurret();
 //   RotateGun();
}

GameObject MoveTurret()
{
    enemies = GameObject.FindGameObjectsWithTag(Enemytag);
    foreach (GameObject enemy in enemies)
    {
        float dist = Vector3.Distance(player.transform.position, enemy.transform.position);
        float minDistance = Mathf.Infinity;

        if (dist < minDistance)
            {
                Rigidbody clone;
                Rigidbody cloneRight;

                minDistance = dist;
                CurrentTarget = enemy;
                target = CurrentTarget.transform.position;
                transform.LookAt(new Vector3(target.x, transform.position.y, target.z));
                if (minDistance == dist && CurrentTarget == enemy && dist <= range)
                {
                    clone = Instantiate(projectile, rightGun.transform.position, transform.rotation) as Rigidbody;
                    cloneRight = Instantiate(projectile, leftGun.transform.position, transform.rotation) as Rigidbody;
                    clone.velocity = transform.TransformDirection(Vector3.forward * 10);
                    cloneRight.velocity = transform.TransformDirection(Vector3.forward * 10);
                }
            }
        

    }

return CurrentTarget; }

/**GameObject MoveGun()
{
    enemies = GameObject.FindGameObjectsWithTag(Enemytag);
    foreach (GameObject enemy in enemies)
    {
        float dist = Vector3.Distance(player.transform.position, enemy.transform.position);
        var minDistance = Mathf.Infinity;
        float Distance = Mathf.Infinity;

        if (count == 1)
        {

            minDistance = dist;
            CurrentTarget = enemy;
            target = CurrentTarget.transform.position;
            turretGun.transform.LookAt(new Vector3(transform.position.x, target.y, transform.position.z));
            count++;
        }
        else
        {
            if (dist < Distance)
       **//**     {
                minDistance = dist;
                CurrentTarget = enemy;
                target = CurrentTarget.transform.position;
                turretGun.transform.LookAt(new Vector3(transform.position.x, target.y, transform.position.z));

            }
        }

    }
    return CurrentTarget;
}
void RotateGun()
{
    guns.transform.Rotate(0, 0 * Time.deltaTime * 1, 220);

}**/

}

My suggestion would be some kind of isTargetting toggle. So if it is shooting at 1 enemy, it wont stop until that enemy is either dead, or is outside it’s max range. Other than that I got nothing, sorry :frowning:

All you need to do is exit your foreach loop over the enemies when you fire a bullet:

 GameObject MoveTurret()
 {
     enemies = GameObject.FindGameObjectsWithTag(Enemytag);
     foreach (GameObject enemy in enemies)
     {
         float dist = Vector3.Distance(player.transform.position, enemy.transform.position);
         float minDistance = Mathf.Infinity;
         if (dist < minDistance)
             {
                 Rigidbody clone;
                 Rigidbody cloneRight;
                 minDistance = dist;
                 CurrentTarget = enemy;
                 target = CurrentTarget.transform.position;
                 transform.LookAt(new Vector3(target.x, transform.position.y, target.z));
                 if (minDistance == dist && CurrentTarget == enemy && dist <= range)
                 {
                     clone = Instantiate(projectile, rightGun.transform.position, transform.rotation) as Rigidbody;
                     cloneRight = Instantiate(projectile, leftGun.transform.position, transform.rotation) as Rigidbody;
                     clone.velocity = transform.TransformDirection(Vector3.forward * 10);
                     cloneRight.velocity = transform.TransformDirection(Vector3.forward * 10);
                     return; // Added this line
                 }
             }
     }
}