Author: Oussama Bouanani

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


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

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.



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.


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.

Simple 2D Physics Game with Unity (Part 2): Main Mechanics – 2D Rigidbody

In this part we’ll create the main game mechanics. As I mentioned in the introduction, we need to save the pandas by eliminating the snakes in each level which is done by sending them to the screen limit that we’ve created in the first part. At the same time we can click on penguins and make them disappear (destroy the penguin objects).


Let’s start by adding the Panda object:

1) Search for “panda” in the project tab then drag one of the sprites and drop it into the scene view. Call the newly created object: “Panda”.


2) Re-size the “Panda” object to (X = 0.5, Y = 0.5, Z = 0.5) so that it fits our demo level and place it on top of the platforms.

3) In the inspector, Add Component -> “2D Box Collider” to add a collider for the “Panda” object. Make sure that “Is Trigger” is un-checked.


So how are we going to make the objects fall? The answer is the “Rigidbody 2D” component. And how does it work exactly?

4) In the inspector, Add Component -> “Rigidbody 2D”. This rigidbody component makes the object that it’s attached to affected by gravity.


So when you play the game at this stage, you will notice that the “Panda” object starts falling down till it reaches the platforms and it stops. The rigidbody puts the object under the control of the physics engine so the gravity pulls the “Panda” object down till it stops when the “Panda” object collider collides with the platform and since both of the colliders have “Is Trigger” un-checked, then both of them won’t ignore this collision and that’s how the “Panda” object stopped falling. We don’t want the platforms that we’ve created in the first part to be affected by gravity so we didn’t add a rigidbody to them. Adding a rigidbody to the object also means that we can control the object by applying forces from the scripts (which we will probably cover in future tutorials). However, if “Is Kinematic” is checked then the rigidbody will stop reacting to collisions and applied forces.


The rigidbody component has a few more settings (gravity scale, mass, etc…) that you can see in details here.

Now let’s move the “Panda” object so that it’s not on top of the platforms anymore and hit the play button. You will notice that the object falls till it gets destroyed. But how? It’s because the object reached the screen limit which destroys the object that enters in its collider.


Enough playing with the Panda, let’s add the rest of the objects. Simply repeat the first steps above to create both the Snake and the Penguin objects.

Now duplicate the Penguin object twice and put all the objects just like in the screenshot below:



Next, we’ll create the script that will handle all these different objects and which will allow us to destroy the “Penguin” objects by clicking on them.

1) Right-click in the project tab, create a new C# script and call it “ObjectManager”.


2) Open the script and let’s add our first variables:

3) Add the “OnMouseDown()” message below to the script. This message is only called when the player has pressed the object that includes this script. We first check if it’s the penguin object (that can be destroyed when clicked), if yes, then we destroy it. If not, nothing happens.

4) Add the “ObjectManager” script to each one of the animal objects in the scene: Set “Click To Destroy” to true for the penguins, “ObjToKeep” for the panda, “ObjToEliminate” for the snake.


You can also add more and more objects of these 3 types to make different levels, just make sure you add the “ObjectManager” script to each one of them and set the object type from the inspector.


Now if you play the game, you can click on the penguins to destroy them. If you destroy the penguin in the right, the panda object falls down to the screen limit and if you click on the penguin in the left, the snake object falls down to the screen limit and gets destroyed.But there’s no indication that we’ve won or lost the game by protecting the panda object or failing to protect it. And that’s what we’re going to do in the next part!

Please don’t hesitate to ask any question and share your suggestions in the comments below.

Simple 2D Physics Game with Unity (Part 1): Introduction – Level Design – 2D Colliders


Are you a Unity beginner? Looking to learn about 2D Physics? Then this tutorial is for YOU! You’ll learn how to create a fun, simple 2D Physics based game with Unity.

The GIF below will explain exactly how the game we want to make works: The goal of the game is to save the panda(s) (it could be one or it could be many at the same time) by eliminating the snakes in each level which is done by making them fall from the camera view. At the same time, you can click on the penguins to make them disappear and help eliminate the snakes.


Level Design – 2D Colliders:

We will start by making a simple level design. In this tutorial, I decided to use Kenney’s 2D Platformer Pack which is available for free Kenney’s 2D Animals Pack. Make sure you check Kenney’s other free assets and support him to continue creating these awesome assets.

1) Create a new Unity Project.

2) Import the 2D Platformer Pack and the Animals Pack to Unity.

3) Search for “Background” in the project and you should find a folder called “Backgrounds”. Double-click on that folder and choose the background sprite you’d like to use.


4) Drag and drop the background sprite into the scene view to create the first background object, call it “Background” then place it in the (X= 0,Y= 0,Z= 5) position.

Duplicate the “Background” object twice then place the first one in the (X= 10,Y= 0,Z= 5) position and the second one in the (X= -10,Y= 0,Z= 5) position. Now you should see this in your scene view:


5) Search for “grass” in the project and you should find a many grass platform sprites. Pick one, drag it into the scene, call it “Platform” then place it in the (X=0,Y=-1.5,Z=0) position.

6) Duplicate the “Platform” object as many times as you need to create something like this (by moving the duplicates):


7) Drag and drop all the “Platform” object duplicates to the first “Platform” object in the Hierarchy to make them its child objects.

8) We need the platforms to stop objects that are falling so that they don’t leave the camera view as you can see in the GIF above. This is why we need the “Collider” component.

Select the parent “Platform” object in the Hierarchy. Add Component -> “Box Collider 2D” in the inspector so that it responds to collisions with other objects that include a collider.


This means that this collider will allow other objects, which have a collider component, to detect the platform objects and interact with it. For example, if we have an object that falls on the platforms, it will simply go through them if both of them didn’t have a collider component.

Make sure that “Is Trigger” is un-checked, otherwise, the player will ignore the platforms and pass through them while falling even if both objects have colliders.

There are different types of 2D colliders so the choice of the shape of the collider depends on the shape of the sprite, it can be a simple one like a box or circle collider and it can be a Polygon collider for the complicated shapes. In our case, the platforms are shaped in squares, that’s why we chose the box collider.

You can change the size of the “Box Collider 2D” so that it covers the whole platforms. A size of (X= 3.85, Y= 1.25) should be enough in our case. You may ask why did I chose to place all the platform objects as child object of the one in the middle then add one collider to it to cover all the other platforms next to it. The answer is simple, sometimes objects get stuck between colliders that are placed next to each other. So it’s always recommended to use one collider in these cases. You can also add a collider to each one of the platforms but I recommend that you enlarge the collider’s size (on the x-axis) for each one so that it covers a little bit of the two platforms next to it to avoid objects getting stuck between them.


9) Now if objects don’t get blocked by the platforms, we need add something that will let us that they’ve left the screen view:

Create an empty object and place it in the  (X= 0,Y= -6,Z= 0) position and call it “Screen Limit”

Select the “Screen Limit” object in the Hierarchy. Add Component -> “Box Collider 2D” in the inspector.


Change the size of this collider so that it covers the whole background bottom limit.


In the “Box Collider 2D”, check “Is Trigger”. Why check it this time? Because we want the objects that leave the screen view to pass through the collider. Only then we will be able to detect that an object has left the screen view. Else it will just stay above the “Screen Limit”.

To help us detect the objects that pass through this screen limit, we will use the “OnTriggerEnter2D(Collider2D)” message which is sent when another object enters a trigger collider (that has “Is Trigger” checked) attached to this object. Now right-click in the project tab to create a new C# script, call it “ScreenLimit”, open it and let’s write our very first lines of code in this tutorial:


Each time an objects passes through the “Screen Limit” object’s collider, a message will be sent in “OnTriggerEnter2D” and we will destroy this object. Add this code in the “ScreenLimit” script:

Please note that this is simply a demo level design, you can change the background and platforms sprites and their placement in the level as you wish.

The next part will cover creating the animal objects as well as the “Rigidbody 2D” component.

Please don’t hesitate to ask any question and share your suggestions in the comments below.