In the third part of this tutorial series, we’re going to work on the game manager! What’s the game manager, you ask? Well, it’s the part of the game that will determine whether we won or lost the game:
– Winning Conditions: The player has kept all the objects that he’s supposed to protect (the pandas) over the platforms and has thrown all the objects he’s supposed to eliminate from the screen view (the snakes).
– Loosing Condition: An object that the player is supposed to protect has fallen to the screen limit.
And after winning or loosing the game, we will display a simple message to the player.
Let’s start by creating the UI message.
1) Create a new UI Text object: GameObject -> UI -> Text
2) A new canvas (which is the parent object of all UI objects) and a new UI text object will be created in the scene. Call the text object: “EndGameMessage”.
Select the UI text object and place it anywhere you want in the canvas then change the text color, font, font size and play with the other settings:
3) Open the “ScreenLimit” which will be the script that manages the game and determines if we’ve lost or won.
In order to call and modify the UI elements from the script, make sure you add the line below at the top of the script:
“using UnityEngine.UI;” CODE
Then create the new variable that will hold the end game message, the variable type is set to “Text” because we are dealing with a text UI object:
“public Text EndGameMesssage;” CODE
4) Go back to the Unity Editor and drag and drop the “EndGameMessage” to its field in the “ScreenLimit” script then disable the “EndGameMessage” object.
The next thing to do is to determine when the player looses and then stop the game:
1) Open the “ScreenLimit” script.
2) Go to the “OnTriggerEnter2D” message and before destroying the object that enters in collision with the screen limit, we will check if the object to be removed is an object that we’re not supposed to eliminate (in other words, if it’s a panda):
1 |
if(other.GetComponent<ObjectManager>().ObjToKeep == true) |
If it’s true, then we will enable the “EndGameMessage” and change its value:
1 2 |
EndGameMesssage.gameObject.SetActive(true); //Enable the end game message. EndGameMesssage.text = "You loose!"; //Inform the player that he has lost. |
Finally we’ll stop the game by setting the time scale to 0 which is the scale at which the time is passing. Setting it to 0 will stop the physics engine from working and therefore, no more objects will fall or move unless we reload the level:
1 |
Time.timeScale = 0; |
And this is how the “OnTriggerEnter2D” message should look like at this stage:
1 2 3 4 5 6 7 8 9 10 11 12 |
void OnTriggerEnter2D (Collider2D other) { if(other.GetComponent<ObjectManager>().ObjToKeep == true) //If we're no supposed to eliminate this object: { //We loose the game. EndGameMesssage.gameObject.SetActive(true); //Enable the end game message. EndGameMesssage.text = "You loose!"; //Inform the player that he has lost. Time.timeScale = 0; //Stopping the time so that no more objects can move/fall unless the level is reloaded. } Destroy (other.gameObject); //Destroying the object that reaches the screen limit. } |
Now play the game and try to make the panda object fall to the screen message and you should see a “You loose!” message on your screen.
At the same time, we’ll also be checking each time an object arrives at the screen limit if the player has satisfied the winning conditions.
To do that, we’ll check if the player has eliminated all the snakes objects: we’ll need to count the all the snakes object when the game starts then store that amount in an int variable (“ObjectsToEliminateAmount”). Only then we will create another int variable and set it to “0” (“ObjectsToEliminateCount”) as soon the game starts. Create the variables first:
1 2 |
public int ObjectsToEliminateAmount; public int ObjectsToEliminateCount; |
Then add the “Awake()” message:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void Awake () { ObjectsToEliminateCount = 0; //Set the objects to eliminate count to 0. ObjectsToEliminateAmount = 0; ObjectManager[] AllObjs = FindObjectsOfType(typeof(ObjectManager)) as ObjectManager[]; //Search for all the objects that have the object manager script. foreach(ObjectManager Obj in AllObjs) //Loop through all these objects. { if(Obj.ObjToEliminate == true) //For each time we find an object to eliminate... { ObjectsToEliminateAmount += 1; //...we will add 1 to the objects to eliminate amount. } } } |
Now in the “OnTriggerEnter2D” message, wee will check if the object to be removed is an object that we’re supposed to eliminate (in other words, if it’s a snake):
1 |
if(other.GetComponent<ObjectManager>().ObjToEliminate == true) |
If it’s true, we will simply increase the eliminated snakes count by 1:
1 |
ObjectsToEliminateCount += 1; |
Right after that, we’ll check if we eliminated all the snakes objects or not:
1 |
if(ObjectsToEliminateCount == ObjectsToEliminateAmount) |
If it’s true, we will announce to the player that he has won by showing the winning message and stopping setting the time scale to 0 as well
1 2 3 |
EndGameMesssage.gameObject.SetActive(true); EndGameMesssage.text = "You WIN!"; Time.timeScale = 0; |
This is how the “OnTriggerEnter2d” message should look like now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
void OnTriggerEnter2D (Collider2D other) { if(other.GetComponent<ObjectManager>().ObjToKeep == true) //If we're no supposed to eliminate this object: { //We loose the game. EndGameMesssage.gameObject.SetActive(true); //Enable the end game message. EndGameMesssage.text = "You loose!"; //Inform the player that he has lost. Time.timeScale = 0; //Stopping the time so that no more objects can move/fall unless the level is reloaded. } if(other.GetComponent<ObjectManager>().ObjToEliminate == true) //If we're supposed to eliminate this object: { ObjectsToEliminateCount += 1; //Increase the count by 1. if(ObjectsToEliminateCount == ObjectsToEliminateAmount) //If we've eliminated all the objects to win { //Win the game. EndGameMesssage.gameObject.SetActive(true); //Enable the end game message. EndGameMesssage.text = "You WIN!"; //Inform the player that he has won. Time.timeScale = 0; //Stopping the time so that no more objects can move/fall unless the level is reloaded. } } Destroy (other.gameObject); //Destroying the object that reaches the screen limit. } |
Now play the game and try to make the snake object fall and keep the panda object on the platforms and you should see a “You win!” message.
You can add more pandas, more snakes and more penguins and you will only win the game by eliminating all the snakes and loose it if only one of the pandas has entered the screen limit.
Guess what? you have a fully functional 2D physics based game! More parts will come soon where I will show you how to add more variation to the level and how to add a main menu for the game.
You can download the full project (Minimum version: Unity 5.0.0f) here.
Please don’t hesitate to ask any question and share your suggestions in the comments below.