Creating Collectibles

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:

  1. Create a new Particle System by going into GameObject | Particle System. Change the name of the object to Collectible and center it in the world by changing its position to 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:
    Creating Collectibles
  2. Then, select the Collectible game object in the Hierarchy again and then select the Tag as Orb. Under the Shape section, change the Shape variable to Sphere and the Radius to 0.01:
    Creating Collectibles
  3. Click on the Arrow on the right-hand side of Start Lifetime and change the values to be Random Between Two Constants. Change those values to 0 and .2. Do the same with Start Speed between 0 and 1. Make Start Size use random values between 0 and 1.5:
    Creating Collectibles
  4. Under Start Color, change the value to yellow by clicking on the color to bring up the Color select dialogue. Once there set the alpha (A) to 39:
    Creating Collectibles
  5. Next, click on the Emission section to open it up, and from there, set the Rate to 100.
  6. Now add a Sphere Collider by selecting Component | Physics | Sphere Collider. Inside of the Inspector, set the Is Trigger Boolean to true, set the Center to (0, 0, 0) and then set the Radius to 0.4:
    Creating Collectibles
  7. Finally, drag and drop the Collectible object into the Prefabs section of the Project folder and delete the object in the Hierarchy:

Adding new objects to our level builder

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;

Note

Note that in this case, I added in a Header as well, which will help you convey what these variables actually are.

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;
                }
                
            }
        }
    }

Switch statements continued

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:

  1. After this, we need to modify our level array to actually have the collectibles and player in it. Replace one 0s in your level to a 2 to put the player there and add in some 3s for the player to collect. Mine looks like the following:
    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} 
    };
  2. Save your script and exit back into the editor. Select your Game Controller and then assign the Player and Orb variables with the appropriate prefabs:
    Switch statements continued
  3. Finally, save your scene and run the game:
    Switch statements continued

    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.

  4. After we have the physical representation of the object done, let's now implement its functionality. Create a new script in our Scripts folder, which we will name 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);
        }
    }
  5. Assign the OrbBehaviour component to the Orb prefab by going to the Project tab and opening the Prefabs folder and selecting the Collectible object. From there, scroll all the way down on the Inspector tab, click on Add Component, and start typing in Orb and then hitting the Enter key:
    Switch statements continued
  6. Save the scene and then play the game:
    Switch statements continued

And with this, we can now collect orbs and they dissapear when we touch them!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset