Simple 2D Physics Game with Unity (Part 3): Game Manager

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

SimplePhysicsGame-Part3-CreatingUIText

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:

SimplePhysicsGame-Part3-ModifyingMessage




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.

SimplePhysicsGame-Part3-LinkingMessage

 

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):

If it’s true, then we will enable the “EndGameMessage” and change its value:

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:

And this is how the “OnTriggerEnter2D” message should look like at this stage:

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.

SimplePhysicsGame-Part3-YouLoose1

 




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:

Then add the “Awake()” message:

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):

If it’s true, we will simply increase the eliminated snakes count by 1:

Right after that, we’ll check if we eliminated all the snakes objects or not:

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

This is how the “OnTriggerEnter2d” message should look like now:

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.

SimplePhysicsGame-Part3-YouWin1

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.