// Checks collision between foot and enemies. private void OnTriggerEnter2D(Collider2D collision) { // Gets component of left enemy (snake which moving left direction). EnemyLT enemyLT = collision.gameObject.GetComponent <EnemyLT>(); // Gets component of right enemy (snake which moving right direction). EnemyRT enemyRT = collision.gameObject.GetComponent <EnemyRT>(); // if the object is collected then give the boolean a value of true // if the object is collected then give the boolean a value of true if (collision.gameObject.CompareTag("EnemyRT") || collision.gameObject.CompareTag("EnemyLT")) { isEnemyDestroyed = true; // If collision is detected between foot and left snake then destroy the enemy. if (collision.gameObject.CompareTag("EnemyLT")) { StartCoroutine(enemyLT.DestroyEnemies()); } // If collision is detected between foot and right snake then destroy the enemy. if (collision.gameObject.CompareTag("EnemyRT")) { StartCoroutine(enemyRT.DestroyEnemies()); } } else { return; } } // End of OnTriggerEnter2D
}// End of Update method. // Checks collision between leg and enemies. private void OnTriggerEnter2D(Collider2D collision) { // Gets component of left enemy (snake which moving left direction). EnemyLT enemyLT = collision.gameObject.GetComponent<EnemyLT>(); // Gets component of right enemy (snake which moving right direction). EnemyRT enemyRT = collision.gameObject.GetComponent<EnemyRT>(); //healthBar = collision.gameObject.GetComponent<HealthBar>(); healthBar = FindObjectOfType<HealthBar>(); // if the object is collected then give the boolean a value of true // if the object is collected then give the boolean a value of true if (collision.gameObject.CompareTag("EnemyRT") || collision.gameObject.CompareTag("EnemyLT")) { isEnemyDestroyed = true; // When collides with left enemy (snake which moving left direction) then take away five points from health bar. if (collision.gameObject.CompareTag("EnemyLT")) { //healthBar.showHealth(); healthBar.DealDamage(5); StartCoroutine(enemyLT.DestroyEnemies()); } // When collides with right enemy (snake which moving right direction) then take away five points from health bar. if (collision.gameObject.CompareTag("EnemyRT")) { healthBar.DealDamage(5); StartCoroutine(enemyRT.DestroyEnemies()); } } else return; }// End of method OnTriggerEnter2D