IEnumerator InCombat(AIarcherController enemy) { //keep track of first hit so the unit starts attacking as soon as it collides instead of waiting and then begining to attack if (firstHit == false) { GetComponent <AudioSource>().PlayOneShot(swordClashFX, 0.5f); } if (firstHit == true && health > 0) { GetComponent <AudioSource>().PlayOneShot(swordClashFX, 0.5f); yield return(new WaitForSeconds(attackSpeed)); } firstHit = true; //if this warrior dies during combat then call the DIE method if (health <= 0) { Die(); } //deduce health from the enemy else if (enemy.health > 0 && animator.GetBool("inRange") == true) { enemy.health = enemy.health - attackDamage; StartCoroutine(InCombat(enemy)); } //else the enemy has been killed thus, reset the warrior else { firstHit = false; resetWarrior(); } }
void OnCollisionStay2D(Collision2D col) { if (col.transform.tag == "warrior") { colAnimator = col.transform.GetComponent <Animator> (); Vector2 relativePosition = transform.InverseTransformPoint(col.transform.position); if (relativePosition.x < 0) { colAnimator.SetBool("isIdle", true); } } if (col.transform.tag == "Archer") { colAnimator = col.transform.GetComponent <Animator> (); Vector2 relativePosition = transform.InverseTransformPoint(col.transform.position); if (relativePosition.x < 0) { colAnimator.SetBool("isIdle", true); } } if (col.transform.tag == "AIwarrior" && animator.GetBool("inRange") == false) { AIwarriorController someObject = col.gameObject.GetComponent <AIwarriorController>(); animator.SetBool("inRange", true); animator.SetBool("isIdle", false); StartCoroutine(InCombat(someObject)); } if (col.transform.tag == "AIcastle" && animator.GetBool("inRange") == false) { AIHealthBar castleObject = col.gameObject.GetComponent <AIHealthBar>(); animator.SetBool("inRange", true); animator.SetBool("isIdle", false); StartCoroutine(InCombat(castleObject)); //get the reference of the enemy castle healthbar and pass it to the combat method } if (col.transform.tag == "AIworker" && animator.GetBool("inRange") == false) { AIworkerController workerObject = col.gameObject.GetComponent <AIworkerController>(); animator.SetBool("inRange", true); animator.SetBool("isIdle", false); StartCoroutine(InCombat(workerObject)); //get the reference of the enemy warrior and pass it to the combat method } if (col.transform.tag == "AIarcher" && animator.GetBool("inRange") == false) { AIarcherController someObject = col.gameObject.GetComponent <AIarcherController>(); animator.SetBool("inRange", true); animator.SetBool("isIdle", false); StartCoroutine(InCombat(someObject)); } }
//determine whether a target is in range to start attacking void GetTarget() { target = GameObject.FindGameObjectWithTag("AIwarrior"); if (target == null) { target = GameObject.FindGameObjectWithTag("AIarcher"); } if (target == null) { target = GameObject.FindGameObjectWithTag("AIworker"); } if (target == null) { target = GameObject.FindGameObjectWithTag("AIcastle"); } if (target != null) { distance = Vector3.Distance(transform.position, target.transform.position); targetFound = distance <= maxRange; } if (targetFound == true) { if (target.transform.tag == "AIwarrior") { AIwarriorController AIwarriorObject = target.GetComponent <AIwarriorController>(); StartCoroutine(Shoot(AIwarriorObject)); } if (target.transform.tag == "AIworker") { AIworkerController AIworkerObject = target.GetComponent <AIworkerController>(); StartCoroutine(Shoot(AIworkerObject)); } if (target.transform.tag == "AIarcher") { AIarcherController AIarcherObject = target.GetComponent <AIarcherController>(); StartCoroutine(Shoot(AIarcherObject)); } if (target.transform.tag == "AIcastle") { AIHealthBar AIcastleObject = target.GetComponent <AIHealthBar>(); StartCoroutine(Shoot(AIcastleObject)); } } }
IEnumerator Shoot(AIarcherController enemyArcher) { animator.SetBool("inRange", true); //keep track of first hit so the unit starts attacking as soon as it collides instead of waiting and then begining to attack if (firstHit == false) { GetComponent <AudioSource>().PlayOneShot(bowReleaseSFX, 1); } /*if (firstHit == true && health > 0) { * GetComponent<AudioSource>().PlayOneShot (bowReleaseSFX, 1); * yield return new WaitForSeconds (attackSpeed); * }*/ firstHit = true; //if this warrior dies during combat then call the DIE method if (health <= 0) { Die(); } //deduce health from the enemy else if (enemyArcher.health > 0 && animator.GetBool("inRange") == true) { GetComponent <AudioSource>().PlayOneShot(bowReleaseSFX, 1); Instantiate(_arrow, thisArcher.transform.position, Quaternion.identity); yield return(new WaitForSeconds(attackSpeed)); StartCoroutine(Shoot(enemyArcher)); } //else the enemy has been killed thus, reset the warrior else { firstHit = false; animator.SetBool("inRange", false); } }