At this point, we have the basis of our game, but now we need to add some gameplay to our world. Perhaps we will have it so that we need to collect all of the coins in the level and then the goal will open:
0
, 0
, 0
. Next, we need to assign the object's tag to Orb. To do so, select Tag | Add Tag. Once at the Tag menu, press the + button to the right of Tags and then from the assign Tag 0, put in Orb and press Enter:0
and .2
. Do the same with Start Speed between 0
and 1
. Make Start Size use random values between 0 and 1.5:true
, set the Center to (0, 0, 0
) and then set the Radius to 0.4:Now, we need to modify our original Build Level
function to have support in order to add these collectibles as well to our level. While we're at it, let's make it so the level can spawn the Player
as well. Rename our player object to Player
if not done yet and drag and drop it to the Prefabs
folder as well. After we've created the prefabs, go ahead and delete the Player
object.
So because we're writing a new function, we have some new variables to introduce in the GameController
class:
[Header("Object References")] public Transform wall; public Transform player; public Transform orb;
Then change the BuildLevel
function replacing what was inside our for loops to:
void BuildLevel () { // Get the DynamicObjects object so we can make it our // newly created objects' parent GameObject dynamicParent = GameObject.Find("Dynamic Objects"); // Go through each element inside our level variable for (int yPos = 0; yPos < level.Length; yPos++) { for (int xPos = 0; xPos < (level[yPos]).Length; xPos++) { Transform toCreate = null; switch(level[yPos][xPos]) { case 0: // Do nothing because we don't want anything // in this place break; case 1: toCreate = wall; break; case 2: toCreate = player; break; case 3: toCreate = orb; break; default: print("Invalid number: " + (level[yPos][xPos]).ToString()); break; } if(toCreate != null) { Transform newObject = Instantiate(toCreate, new Vector3(xPos, (level.Length - yPos), 0), toCreate.rotation) as Transform; // Set the object's parent to the DynamicObjects // variable so it doesn't clutter our Hierachy newObject.parent = dynamicParent.transform; } } } }
As we described briefly previously in Chapter 3, GUIs Part 2 – Clicker Game, a switch
statement can be thought of as a nice way to compare a single variable against a number of different values. As the case here, the switch statement could be rewritten as:
if(level[yPos][xPos] == 0) { } else if(level[yPos][xPos] == 1) { toCreate = wall; } else if(level[yPos][xPos] == 2) { toCreate = player; } else if(level[yPos][xPos] == 3) { toCreate = orb; } else { print("Invalid number: " + (level[yPos][xPos]).ToString()); }
However, surely you can tell that writing it as a switch
statement is much nicer to look at and requires less code duplication, which is something we want to reduce as much as possible:
private int[][] level = new int[][] { new int[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, new int[]{1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 0, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, new int[]{1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, new int[]{1, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, new int[]{1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, new int[]{1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 3, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, new int[]{1, 0, 0, 0, 3, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1}, new int[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, new int[]{1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, new int[]{1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, new int[]{1, 0, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, new int[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} };
And there we go! Our player as well as collectibles are now spawning via our BuildLevel
function. Finally, let's make it so we can actually collect these.
OrbBehaviour
. Open it up in MonoDevelop
and fill it in with the following:using UnityEngine; public class OrbBehaviour : MonoBehaviour { void OnTriggerEnter(Collider other) { Destroy(this.gameObject); } }
And with this, we can now collect orbs and they dissapear when we touch them!