Example #1
0
    }     // end of fixed update method

    //this is called whenever the enemy hits something
    void OnCollisionEnter2D(Collision2D other)
    {
        //Check to see that what we hit was the player's bullet
        if (other.gameObject.tag == "PlayerBullet")
        {
            //Since it got shot, we might drop a power up
            //first, get a number based on the drop chance and the random number generator
            //get a number between 0 and 100
            int dropRoll = Random.Range(0, 100);
            //compare the roll with dropChance, if the roll is lower than chance it was successful
            if (dropRoll <= dropChance)
            {
                //drop the power up right there
                Instantiate(powerUpPreFab, transform.position, Quaternion.identity);
            }

            //give the player 100 points for killing the enemy
            GameControl.Add_Score(100);

            //create the explosion prefab at the location of the enemy
            Instantiate(explosion, transform.position, Quaternion.identity);
            //send information to the debug, this is just for testing purposes
            Debug.Log("Enemy hit by " + other.gameObject.name);

            //Take the enemy out of the count of enemies
            GameControl.Add_Enemy(-1);

            //destroy both the bullet and the enemy
            Destroy(other.gameObject);
            Destroy(gameObject);
        }

        //if we run into the player
        if (other.gameObject.tag == "Player")
        {
            //create an explosion at the collision center
            Instantiate(explosion, transform.position, Quaternion.identity);

            //killing an enemy by running into is worth half points
            GameControl.Add_Score(50);

            //Take the enemy out of the count of enemies
            GameControl.Add_Enemy(-1);

            //destroy the enemy - add code to hurt the player later
            Destroy(gameObject);
        }

        //if the enemy makes it to the bottom of the screen
        if (other.gameObject.tag == "Border")
        {
            //Take the enemy out of the count of enemies
            GameControl.Add_Enemy(-1);

            //just disappear
            Destroy(gameObject);
        }
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        //throw this in so that if the game is paused, we don't spawn enemies
        if (Time.timeScale == 0)
        {
            //return command ends the current method and ignores everything under it
            return;
        }
        //count down the counter by one each frame
        count--;

        //count down is over, lets spawn an enemy
        if (count <= 0)
        {
            //Make sure we don't have too many enemies already
            // the != means does not equal
            if (enemyCount != enemyLimit)
            {
                //change the position based on which enemy it is, to give some variety
                //at this point, I don't vary the position at all
                //float variance = Random.Range(0f, 0.3f);
                Vector2 enemyPos = new Vector2(transform.position.x, transform.position.y);

                //Instantiate the enemy type
                Instantiate(enemyType, enemyPos, Quaternion.identity);

                //Tell the GameControl we made another enemy
                GameControl.Add_Enemy(1);

                //update the count of enemies
                enemyCount++;

                //reset the counter
                count = delayCount;
            }
        }
    }