IEnumerator AttackTargetRepeatedly() { //determine if alive (Attacker or defender) bool attackerStillAlive = GetComponent <HealthSystem>().healthAsPercentage >= Mathf.Epsilon; bool targetStillAlive = target.GetComponent <HealthSystem>().healthAsPercentage >= Mathf.Epsilon; //while still alive while (attackerStillAlive && targetStillAlive) { //know how often to attack var animationClip = currentWeaponConfig.GetAnimClip(); float animationClipTime = animationClip.length / character.GetAnimatorSpeedMultiplier(); //float timeToWait = animationClipTime + currentWeaponConfig.GetMinTimeBetweenAnimationCycles(); float timeToWait = currentWeaponConfig.GetTimeToWaitBetweenHits(); //if time to hit again bool isTimeToHitAgain = Time.time - lastHitTime > timeToWait; if (isTimeToHitAgain) { AttackTargetOnce(); lastHitTime = Time.time; } yield return(new WaitForSeconds(timeToWait)); } }