コード例 #1
0
        /// Collider for enemy prefab
        /// Upon contact with player, initiates Player class' LoseHealth() method
        /// Destroys itself upon contact to laser or player.
        private void OnTriggerEnter2D(Collider2D other)
        {
            if (other.tag == "Laser" || other.tag == "Player")
            {
                Laser laser1 = other.GetComponent <Laser>();

                if (laser1 != null)
                {
                    if (bossHealth > 0)
                    {
                        bossHealth--;
                    }

                    else
                    {
                        Instantiate(enemyExplosionPrefab, transform.position, Quaternion.identity);
                        StartCoroutine(DeleteRoutine());
                    }
                }

                Player player1 = other.GetComponent <Player>();

                if (player1 != null)
                {
                    if (bossHealth > 0)
                    {
                        bossHealth--;
                        player1.LoseHealth();
                    }

                    else
                    {
                        player1.LoseHealth();

                        Instantiate(enemyExplosionPrefab, transform.position, Quaternion.identity);
                        StartCoroutine(DeleteRoutine());
                    }
                }
            }
        }
コード例 #2
0
        /**
         * Collider for enemyLaser.
         * Upon contact with player, calls its' LoseHealth() method.
         */
        private void OnTriggerEnter2D(Collider2D other)
        {
            if (other.tag == "Player")
            {
                Player player1 = other.GetComponent <Player>();

                if (player1 != null)
                {
                    player1.LoseHealth();
                    Destroy(this.gameObject);
                }
            }
        }