Why don’t we use a coroutine to keep up with the time needed? Instead of counting deltaTime?
Sorry I missed this.
I’m guessing what you are asking is that rather than using a timer and adding time.deltaTime, preventing attacks until the timer exceeds the time between attacks, that we start a coroutine with a yield return new WaitForSeconds(timeBetweenAttacks) (at which time, a bool would be set to true allowing an attack)
You could do this, if you wish, but it’s probably a bit more work.
I have the same thought.
So here is my code.
bool isAttacking = false;
private void Update()
{
if (target == null)
return;
if (isInRange())
{
mover.Cancel();
if (isAttacking == false)
{
StartCoroutine(TriggerAttackAnimation());
isAttacking = true;
}
}
else
{
mover.MoveTo(target.position);
}
}
IEnumerator TriggerAttackAnimation()
{
while (target != null)
{
Debug.Log("Trigger attack animation");
animator.SetTrigger("attack");
yield return new WaitForSeconds(attackInterval);
}
yield break;
}
note that the isAttacking is necessary because without it would start a new coroutine every frame.
You’ll also need to add StopAllCoroutines(); to the else statement in Update();
The Cancel() method should also have StopAllCoroutines();
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.