for some reason, when my enemy shoots a projectile 5 times it doesnt set the canBarrage to false and so keeps on firing even though the for loop says that it should only be done barrageNumber of times (which i set to 5 in the inspector)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurretEnemy : Log
{
public GameObject projectile;
public float fireDelay;
private float fireDelaySeconds;
public float barrageDelay;
private float barrageDelaySeconds;
public bool canFire = true;
public int barrageNumber;
public bool canBarrage = true;
private void Update()
{
fireDelaySeconds -= Time.deltaTime;
if(fireDelaySeconds <= 0)
{
canFire = true;
fireDelaySeconds = fireDelay;
}
barrageDelaySeconds -= Time.deltaTime;
if(barrageDelaySeconds <= 0)
{
canBarrage = true;
barrageDelaySeconds = barrageDelay;
}
}
public override void CheckDistance()
{
if (Vector3.Distance(target.position,
transform.position) <= chaseRadius
&& Vector3.Distance(target.position,
transform.position) > attackRadius)
{
if (currentState == EnemyState.idle || currentState == EnemyState.walk
&& currentState != EnemyState.stagger)
{
if (canBarrage)
{
for (int i = 0; i < barrageNumber; i++)
{
if (canFire)
{
Vector3 tempVector = target.transform.position - transform.position;
GameObject current = Instantiate(projectile, transform.position, Quaternion.identity);
current.GetComponent<Projectile>().Launch(tempVector);
canFire = false;
ChangeState(EnemyState.walk);
anim.SetBool("wakeUp", true);
Debug.Log(i);
}
else
{
return;
}
}
canBarrage = false;
}
}
}
else if (Vector3.Distance(target.position,
transform.position) > chaseRadius)
{
anim.SetBool("wakeUp", false);
}
}
}