Doing it as a countdown

Usually I’d rather use the timeBetweenAttacks as an initializer value for a countdown timer.

So I might have something similar to this:

       private void Update()
        {
            if (null == target) return;

            if (attackCooldownTimer > 0)
            {
              attackCooldownTimer -= Time.deltaTime;
              return;
            }

            if (!GetIsInRange())
            {
                StopAttackBehaviour();
                mover.MoveTo(target.position);
            }
            else
            {
                StartAttackBehaviour();
                mover.Cancel();
            }
        }

        private void StartAttackBehaviour()
        {
            attackCooldownTimer = timeBetweenAttacks;
            animator.SetTrigger(ATTACK_TRIGGER);
        }

        private void StopAttackBehaviour()
        {
            animator.ResetTrigger(ATTACK_TRIGGER);
        }

Privacy & Terms