Winning the game

Now that we have the goals showing up, we now need some way in order to complete the project. With this in mind, let's create our final place, a goal, or the player to reach to go to the next level.

  1. Create a new Particle System by going into GameObject | Particle System. Change the name of the object to Goal.
  2. Under the Shape section, change the Shape variable to Box and change Box X to 1 and Box Y and Box Z to 0.
  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 1. Do the same with Start Speed between 2 and 4. Make Start Size use random values between 0 and 0.5. Change the Start Color to a Random Between Two Colors using a green and purple color. Finally, uncheck the Play On Awake variable. Finally, we want the particles to fly up so change Rotation X to -90 or 270:
    Winning the game

    With this, you should have a nice stream of particles coming in for your new goal object!

  4. Add in a Box Collider by selecting Component | Physics | Box Collider. Toggle the Is Trigger option to be true, set the Center to be (0,0,0) and the Size to be (1,1,1):
  5. Now that we have the goal's object completed, let's now have it spawn within our BuildLevel function in our Game Controller. So, same as before, we drag and drop the object to our Prefabs folder and delete the original object. Then, we need to add two new variables for us to use:
    public Transform goal;
    private ParticleSystem goalPS;
  6. After we add the new variables, add in the following in bold to BuildLevel:
        void BuildLevel() 
        {
            // Get the DynamicObjects object so we can make it
            // our newly created objects' parent
            GameObject dynamicParent = GameObject.Find("DynamicObjects");
    
    
            //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 there.
                        break;
                        case 1:
                        toCreate = wall;
                        break;
                        case 2:
                        toCreate = player;
                        break;
                        case 3:
                        toCreate = orb;
                        break;
                        case 4:
                        toCreate = goal;
                        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;
    
                        if(toCreate == goal)
                        {
                        goalPS = newObject.gameObject.GetComponent<ParticleSystem>();
                        }
    /*
    Set the object's parent to the
    DynamicObjects variable so it doesn't clutter our Hierarchy
    */
                        newObject.parent = dynamicParent.transform;
    
                    }
                    
    
                }
            }
        }
  7. We also need to add in a 4 somewhere inside of our level array:
    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, 4, 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} 
    };
  8. Next in the Unity Inspector, go in and assign the Goal object with your prefab of the same name.
  9. With this done, we now need to go in and add in the ability to win. Go into the CollectedOrb function and start our Particle System when we get all of the orbs:
        public void CollectedOrb()
        {
            orbsCollected++;
            scoreText.text = "Orbs: " + orbsCollected + "/" + orbsTotal;
    
            if(orbsCollected >= orbsTotal)
            {
                goalPS.Play();
            }
        }
  10. After that we need to create the script for our goal. Create a new script in our Scripts folder that we will name GoalBehaviour. Open it up in MonoDevelop and fill it in with the following:
    using UnityEngine;
    
    public class GoalBehaviour : MonoBehaviour 
    {
        ParticleSystem ps;
        void Start()
        {
            ps = GetComponent<ParticleSystem>();
        }
    
        void OnTriggerEnter(Collider other) 
        {
            if(ps.isPlaying)
            {
                print("You Win!");
            }
        }
    }
  11. Save the file and attach it do the Goal prefab. With this all done, save the scene and hit the Play button:
    Winning the game

And with this, whenever we collect all of the coins that we've placed in our level, the goal will appear. Finally, when we touch it, the game will tell us that we have won!

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

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