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.
Goal
.Box
and change Box X to 1
and Box Y and Box Z to 0
.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:With this, you should have a nice stream of particles coming in for your new goal object!
true
, set the Center to be (0,0,0
) and the Size to be (1,1,1
):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;
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; } } } }
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,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 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} };
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(); } }
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!"); } } }
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!