Enemy does not stop sooting

So my enemy “Gunner” shots a projectile object,but he shots the projectile always, even he is is Walking_Animation and not into Attack_Animation. I want him to shot the projectile object only if he is is Attack_Animation

public float speed;
public float stoppingDistance;
public float retreatDistance;
public GameObject projectile;
private float timeBtwShots;
public float startTimeBtwShots;
Rigidbody2D rb;
public int maxHealth = 100;
int currentHealth;
private Transform player;
public Animator animations;

void Start()
{
player = GameObject.FindGameObjectWithTag(“Player”).transform;

    rb = GetComponent<Rigidbody2D>();
    currentHealth = maxHealth;

    timeBtwShots = startTimeBtwShots;
}

void Update()
{
if(Vector2.Distance(transform.position, player.position) > stoppingDistance)
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
animations.SetBool(“canWalk”, true);
}
else if (Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance)
{
transform.position = this.transform.position;
animations.SetBool(“canWalk”, false);
}
else if (Vector2.Distance(transform.position, player.position)< retreatDistance)
{
transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
animations.SetBool(“canWalk”, true);

    }
    if (timeBtwShots <= 0)
    {
        Instantiate(projectile, transform.position, Quaternion.identity);
        timeBtwShots = startTimeBtwShots;
        animations.SetBool("Attack", true);
    }
    else
    {
        timeBtwShots -= Time.deltaTime;
        animations.SetBool("Attack", false);
    }
}

And this is the projectile script

public float speed;
[SerializeField] private float attackDamage = 20f;

private Transform player;
private Vector2 target;

void Start()
{
player = GameObject.FindGameObjectWithTag(“Player”).transform;

    target = new Vector2(player.position.x, player.position.y);
}

void Update()
{
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
if(transform.position.x == target.x && transform.position.y == target.y)
{
DestroyProjectile();
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag(“Player”))
{
DestroyProjectile();

        if (other.gameObject.tag == "Player")
        {
            other.gameObject.GetComponent<PlayerHealth>().UpdateHealth(-attackDamage);
        }
    }
    }

void DestroyProjectile()
{
    Destroy(gameObject);
}

Hi!

as far as i can see you don’t have any logic that stops bullets from being fired… you shoud’nt just use update to trigger bullets if you don’t handle an if statement to check wether you should or not shoot a bullet… try something like this

  // by hitting space, you're allowing to shoot a bullet
  if (Input.getKey(KeyCode.Space))
  {
    if (timeBtwShots <= 0) {
  	  Instantiate(projectile, transform.position, Quaternion.identity);
  	  timeBtwShots = startTimeBtwShots;
  	  animations.SetBool("Attack", true);
    } else {
  	  timeBtwShots -= Time.deltaTime;
  	  animations.SetBool("Attack", false);
    }
  }
  else 
  {
	timeBtwShots = startTimeBtwShots;
  }

there’s a lot of room for improvement but i hope this helps you to get on the right path.
Good Luck and keep on coding!