// 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
Example #2
0
    private IEnumerator CreateEnemies()
    {
        while (true)
        {
            // Create and add the enemies
            // Left
            GameObject newEnemyObjLeft = Instantiate(leftEnemyPrefab, GetPlanePositionLeft(), Quaternion.identity, transform);
            EnemyLT    newEnemyLeft    = newEnemyObjLeft.GetComponent <EnemyLT>();

            // Right
            // Create and add the enemies
            GameObject newEnemyObjRight = Instantiate(rightEnemyPrefab, GetPlanePositionRight(), Quaternion.identity, transform);
            EnemyRT    newEnemyRight    = newEnemyObjRight.GetComponent <EnemyRT>();

            // Set up Enemies
            // Left
            newEnemyLeft.mEnemyManager = this;
            allEnemiesLT.Add(newEnemyLeft);

            // Right
            newEnemyRight.mEnemyManager = this;
            allEnemiesRT.Add(newEnemyRight);

            yield return(new WaitForSeconds(timeDelay));

            timeDelay -= increment;
        }
    }
    }// 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