MiniGame2: Pizza Carnage

This game consists of eliminating waves of aliens by throwing pieces of pizza at them.

You will learn to:

  • Create a spawner

  • Use randomness

  • Create and use Prefabs

  • Detect collisions

  • Model a pizza and an alien with their colliders

Scene Layout

  • Open the Assets folder named MiniGames

  • Inside this folder, create a new Scene named: Pizza Carnage

  • Create a ground of 10mx10m, apply a green color

  • Create a capsule to represent the player

  • Create 3 objects to represent the aliens

../_images/farm.png

Player control

  • Create a controller script for the player:

    • Player can move left and right using the arrow keys

    • Add a test to prevent the player from going off the ground

../_images/farmermove.gif

Pizza

Without a doubt, the most interesting part of this project is coming up: modeling a slice of pizza in 3D

../_images/pizza.png
  • Add a script to make the pizza move at a speed of 10m/s in the direction of the aliens

../_images/fly1.gif

Prefab

The player will throw pizza slices to the aliens, so we need to reuse the same slice of pizza multiple times. For this, we transform our 3D pizza slice into a prefab.

  • Drag an drop your pizza slice into the MiniGames folder

../_images/pizzaprefab.png
  • Delete the pizza from the scene

  • Enter Play mode

  • Drag and drop the pizza slice into the hierarchy window, this will instantiate the pizza prefab in real time

../_images/pizzattack.gif
  • Exit Play mode

Firing pizza

  • Go back in the player controller script

    • Add a new variable: public GameObject pizzaSlice

  • Link this variable to your prefab

../_images/link.gif
  • In the update() function, detect when the spacebar is pressed

    if ( Input.GetKeyDown(KeyCode.Space) )
    
  • If we look at the documentation:

    ../_images/docinstantiate.png
  • We notice a version that is interesting:

    • First parameter: the prefab we want to instantiate

    • Its position

    • Its rotation

  • Instantiate a prefab when spacebar is pressed

    • At the player position

    • Using the rotation transform of the prefab

    ../_images/firingseq.gif

Self-destruction

In order to improve game performance, we need to destroy projectiles when they go out of bounds.

  • When a pizza slice if far away (> 10m)

    • Destroy the current object using:

    Destroy(gameObject);
    
    ../_images/fardestroy.gif

Alien Spawner

  • Transform you three aliens into prefab

../_images/newprefab.png
  • Delete aliens from the scene

Our spawn manager script will be associated to a hidden GameObject:

  • Right click in the Hierarchy windows and select: Create Empty

  • Rename this object to spawnmanager

  • Create a script and attach it to the spawn manager

  • Edit the script and add an array of prefabs as a public member:

    public GameObject[] alienPrefabs;
    
  • Select the spawnmanager object

  • In the Inspector window, set the size of the array to 3

  • Drag and drop the 3 alien prefabs to fill the array

  • Create a function Spawn() in the Spawnmanager script

    • Randomly choose the alien prefabs

      int randomValue = Random.Range(0, 3);  // generate a random integer number between 0 and 2
      
    • Make the alien appears in front of the player by randomly choosing its location

    • Call the Spawn() function from the Start() function

  • Enter Play mode:

../_images/R1.png
  • Remove the Spawn() function from the Start() function

  • We now spawn the aliens on a timer

    void Start()
    {
            float startDelay = 2;
            float spawnInterval = 3;
            InvokeRepeating("RandomSpawn",  startDelay, spawnInterval);
    }
    

Collision

  • Double click on the pizza prefab

    • Add a box collider component

      • Click on Edit collider

      • Select scale Tool and reduce the collider to fit the shape of the pizza

      ../_images/editbox.png
  • Go back to the scene

  • Create a new script for aliens and associate this script to each alien prefabs

  • This script must destroy the alien and also the pizza

    Destroy(gameObject);
    Destroy(other.gameObject);
    
  • Configure the collision system so that the game works properly

    • Configure colliders if needed

    • Select the right function for collision management

Here is the final version of the game:

../_images/kill.gif