IEnumerator Shoot(AIHealthBar enemyCastle) { animator.SetBool("inRange", true); 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 (health <= 0) { Die(); } if (enemyCastle.currentHealth > 0 && animator.GetBool("inRange") == true) { GetComponent <AudioSource>().PlayOneShot(bowReleaseSFX, 1); Instantiate(_arrow, thisArcher.transform.position, Quaternion.identity); yield return(new WaitForSeconds(attackSpeed)); StartCoroutine(Shoot(enemyCastle)); } }
private void Start() { gcs = FindObjectOfType <GameControllerScript>(); AITCS = FindObjectOfType <AITankCounterScript>(); AIBHB = FindObjectOfType <AIBossHealthBar>(); AIHB = FindObjectOfType <AIHealthBar>(); }
//combat loop vs castle, same procedure applied as above IEnumerator InCombat(AIHealthBar castle) //overloaded method for case of attacking a Castle { if (firstHit == false) { GetComponent <AudioSource>().PlayOneShot(swordHitCastleFX, 0.5f); } if (firstHit == true && health > 0) { GetComponent <AudioSource>().PlayOneShot(swordHitCastleFX, 0.5f); //play the on-hit sound effect at relatively low volume yield return(new WaitForSeconds(attackSpeed)); } firstHit = true; if (health <= 0) { Die(); } if (castle.currentHealth > 0 && animator.GetBool("inRange") == true) { castle.currentHealth = castle.currentHealth - attackDamage; castle.HandleHealth(); StartCoroutine(InCombat(castle)); } }
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)); } } }
void OnCollisionEnter(Collision collision) { ContactPoint contact = collision.contacts[0]; Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal); Vector3 pos = contact.point; GameObject collidedWith = collision.gameObject; unitName = collidedWith.tag; AIHB = collidedWith.GetComponentInChildren <AIHealthBar>(); if (unitName.Contains("UNIT") && collidedWith != null) { gcs.PlayerHitAi(); if (AITCS.TankCounter >= 1) { AIHB.TakeDemage(TakeDemage); } if (collidedWith.tag == "UNIT-BOSS") { AIBHB.TakeDemage(TakeDemage); } Instantiate(explosionPrefab, pos, rot); Destroy(gameObject); //Destroy(gameObject); if (AIHB.aiIsDead == true) { AITCS.OneKilled(); Destroy(collidedWith); } //if(AIBHB.health <= 0) { // SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); //} } Instantiate(explosionPrefab, pos, rot); Destroy(gameObject); if (unitName.Contains("House")) { Destroy(collidedWith); Destroy(gameObject); } }