Similar to what you did in the EnemyAI class, I challenged myself to do a similar thing here. It works perfectly. I suppose there is not much practical reason to write it like this. I kept the coroutine in case I run into any complications later. I dunno, I kind of like the idea of there being one thread to rule them all.
private float timer = 0f;
private bool isShooting = false;
private int burstCounter =0;
public void Attack(){
if(!isShooting){
//StartCoroutine(ShootRoutine());
isShooting=true;
}
}
private void Update(){
Shoot();
}
private void Shoot(){
if(!isShooting){return;}
if(burstCounter<burstCount && timer>timeBetweenBurstShots){
Vector2 targetDirection = PlayerController.Instance.transform.position - transform.position;
GameObject newBullet = Instantiate(bulletPrefab,transform.position,Quaternion.identity);
newBullet.transform.right=targetDirection;
if(newBullet.TryGetComponent(out Projectile projectile)){
projectile.SetProjectileRangeAndSpeed(projectileRange,projectileSpeed);
}
timer = 0f;
burstCounter++;
}
if(burstCounter>=burstCount && timer>timeBetweenFullBursts){
burstCounter=0;
timer = 0f;
isShooting = false;
return;
}
timer+=Time.deltaTime;
}