// Coroutine to check when to damage the target IEnumerator attack() { // Get the target and subscribe to its alert event attackTarget = playerBehavior.getTarget().GetComponent<KillableInstance>(); attackTarget.alertOnDeath += stopAttacking; // As long as the target is alive... while (attackTarget.isAlive) { // ...if we are in range of it... while (inRangeOfTarget()) { // Wait for a short bit yield return new WaitForSeconds(playerStats.secondsBetweenAttacks / 2); // If we can see the target... if (playerBehavior.seesTarget()) { // ...and we are still in range of it... if (inRangeOfTarget()) { // Start the attacking animation playerAnimations.attackAnimation(true); // Damage the target attackTarget.Damage(playerStats.activeDamage, this.gameObject); // Wait for the animation to finish yield return new WaitForSeconds(playerStats.secondsBetweenAttacks / 2); // Stop the attacking animation playerAnimations.attackAnimation(false); } } // If we can't see the target... else { Debug.Log("player started rotating"); // Pause script execution and rotate towards the target yield return playerBehavior.startRotating(); } } yield return null; } }
// Coroutine to start attacking IEnumerator attack() { if (troopBehavior.targetIsEnemy()) { // Get the target object attackTarget = troopBehavior.getTarget().GetComponent<EnemyStats>(); // Subscribe to the target's death alert attackTarget.alertOnDeath += stopAttacking; // As long as the target is alive, wait for a short bit, then attack while (attackTarget.isAlive) { yield return new WaitForSeconds(troopStats.secondsBetweenAttacks / 2); attackTarget.Damage(troopStats.attackValue, this.gameObject); yield return new WaitForSeconds(troopStats.secondsBetweenAttacks / 2); } // Now that the target is dead, start wandering troopBehavior.changeIntent(this.gameObject); } }