@Brian_Trotter This was how i solved the challenge.
- in start set the time since attack to the cooldown time to avoid any bugs with first attacks.
- created method to increase cooldown but with the check inverted ( i think it should reduce calculations)
- reset time since last attack inside of the attack method instead of standalone in update.
i have three questions…
- does the inverted check actually help optimize here?
- i thought about having the cooldown check manage a a canAttack bool. that way all it is doing is checking canAttack instead of calculating the cooldown directly. I wasn’t sure if that actually optimized or if it really just cost the same amount since its a very small calc.
- am i deviating from the code too much and making lessons further into the course more complicated for myself?
private void Update()
{
ManageAttackCooldown();
if (_target == null) return;
if (_target != null && !GetIsInRange())
{
GetComponent<Mover>().MoveTo(_target.position);
}
else
{
GetComponent<Mover>().Cancel();
if (_timeSinceLastAttack >= _timeBetweenAttacks)
{
AttackBehavior();
}
}
}
private void ManageAttackCooldown()
{
if (_timeSinceLastAttack <= _timeBetweenAttacks)
{
_timeSinceLastAttack += Time.deltaTime;
}
}
public void Attack(CombatTarget combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
_target = combatTarget.transform;
//Hit();
}
(PS, right after your message last night the course refactored those get components i was asking to about into caches at start. so, Im leaving them in here and just trusting the process)