Enemy AI problem. Please help!!!

public float timeBetweenShots = 5.0f;
public float bulletLifeTime = 10.0f;
public bool shoot = true;
public float lifeTime = 5.0f;

    public Rigidbody Enemybullet;
    public Transform muzzlepoint;
     
     
    public float minDist = 10;
    public float maxDist = 5;
     
    public float speed;
    private bool follow; // Follow Enemy Switch
     
     
     
    void Update()
    {

Attack(); }

    // Enemy Attack Function
    private void Attack()
    {
    if((Vector3.Distance(transform.position,target.position)<maxRange)
    && (Vector3.Distance(transform.position,target.position)>minRange))
     
    {
    follow = true; // Enemy Switch Enabled
    }
     
    if(follow) // On enemy Switch Enabled
    {
    transform.LookAt(target);// Locking the target
    transform.Translate(Vector3.forward *speed* Time.deltaTime);// Forward Motion
    transform.Rotate(0,90,0); // Gimber Lock
     
    //Shooting();// Shooting Function is called
     
    if(Vector3.Distance(transform.position,target.position)<= minDist)
    {
    Shooting();
    }
     
     
    }
    }        
     
    // Start shooting Function
    void Shooting()
    {
    shoot = true;
    StartCoroutine("TimerShoot");
    }
     
     
     
    // Timed Shooting
    IEnumerator TimerShoot()
    {
    while(shoot)
    {
    if(Enemybullet)
    {
    Rigidbody newEnemyBullet;
    newEnemyBullet = Instantiate(Enemybullet,muzzlepoint.position,muzzlepoint.rotation) as Rigidbody; // casting and Instantiating bullet.
     
    Physics.IgnoreCollision(collider, newEnemyBullet.collider);
    Destroy(newEnemyBullet.gameObject, bulletLifeTime);
    yield return new WaitForSeconds(timeBetweenShots);
     
    }
    }
    } 

I have many ways but still the bullets are emitted continuously. Is there any way to fix it?,I want the bullet to shoot few times not continuously. Please help. Is there any way that I can stop the enemy to stop following the player once it reached the min distance? AM a newbie how to fix this. Please help.Thanks in advance.

What I think happens is - you wait for a few seconds at the end your TimerShoot() method, but it doesn’t prevent your code from shooting again. Here’s how you could fix it:

Create a new property - bool canShoot = true; .

Change Shooting() code to look like this:

void Shooting()
    {
    shoot = true;

    if(canShoot == true)
    {
        canShoot = false; // here you prevent your object from shooting until TimerShoot() has finished doing its thing
        StartCoroutine("TimerShoot");
    }
    }

Change TimerShoot() to look like this:

    IEnumerator TimerShoot()
    {
    if(Enemybullet)
    {
    Rigidbody newEnemyBullet;
    newEnemyBullet = Instantiate(Enemybullet,muzzlepoint.position,muzzlepoint.rotation) as Rigidbody; // casting and Instantiating bullet.
 
    Physics.IgnoreCollision(collider, newEnemyBullet.collider);
    Destroy(newEnemyBullet.gameObject, bulletLifeTime);
    yield return new WaitForSeconds(timeBetweenShots);
    
    canShoot = true; // enable shooting after timeBetweenShots has been reached
 
    }
    } 

If you want to prevent the enemy from coming too close, you need to add a conditional at the point in your code where you’re moving it. For example:

if(Vector3.Distance(target.position, this.gameObject.transform.position) > minDist)
{
transform.Translate(Vector3.forward *speed* Time.deltaTime);// Forward Motion
}

You should also test your code a bit, because I think it might not work as you’d like it to work. For example, your enemy won’t start shooting until it reaches minimum distance.

if(Vector3.Distance(transform.position,target.position)<= minDist)
    {
    Shooting();
    }

What’s the point of maxDist then?

I haven’t tested that code, so feel free to ask if you encounter any other problems.