I rewrote the burst logic using Update (instead of a coroutine)

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;
    }

Nice! I personally prefer coroutines over updates, but that’s just me. I find it easier and more flexible than just running everything over and over and having to keep track of what I’ve already done and what not.

Coroutines are not multi-threaded. They run on the same thread as updates

1 Like

Thanks. That is good to know. I’ve actually had issues in the past lining up the timings of multiple co-routines, but looking back, that may have been an issue with using “time.Time”, or some other misuse of co-routines. I am reading up on them now. :ok_hand:

Privacy & Terms