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 .. image:: farm.png :scale: 30% 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 .. image:: farmermove.gif :scale: 50% Pizza ===== Without a doubt, the most interesting part of this project is coming up: modeling a slice of pizza in 3D .. image:: pizza.png :scale: 50% * Add a script to make the pizza move at a speed of 10m/s in the direction of the aliens .. image:: fly.gif :scale: 50% 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 .. image:: pizzaprefab.png :scale: 50% * 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 .. image:: pizzattack.gif :scale: 70% * 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 .. image:: link.gif :scale: 80% * In the *update()* function, detect when the spacebar is pressed .. code-block:: csharp if ( Input.GetKeyDown(KeyCode.Space) ) * If we look at the documentation: .. image:: docinstantiate.png :scale: 80% * 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 .. image:: firingseq.gif :scale: 50% 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: .. code-block:: csharp Destroy(gameObject); .. image:: fardestroy.gif :scale: 60% Alien Spawner ============= * Transform you three aliens into prefab .. image:: newprefab.png :scale: 40% * 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: .. code-block:: csharp 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 .. Warning: We remind you that a **Vector3 is a C# structure**, which means that when you write: *Vector3 V2 = V1*, V2 is an independant copy of V1. * Create a function *Spawn()* in the *Spawnmanager* script * Randomly choose the alien prefabs .. code-block:: csharp 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 .. dropdown:: Help in case nothing works :animate: fade-in .. code-block:: csharp void RandomSpawn() { int r = Random.Range(0, 3); Vector3 V = alienPrefabs[r].transform.position; V.z = Random.Range(-6, 6); Instantiate(alienPrefabs[r], V, alienPrefabs[r].transform.rotation); } * Enter Play mode: .. image:: R1.png :scale: 70% * Remove the *Spawn()* function from the *Start()* function * We now spawn the aliens on a timer .. code-block:: csharp 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 .. image:: editbox.png :scale: 40% * 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 .. code-block:: csharp 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: .. image:: kill.gif