Uncategorized

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”.

SimplePhysicsGame-Part2-CreatingPanda

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.

SimplePhysicsGame-Part2-PandaBoxCollider

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.

SimplePhysicsGame-Part2-Panda

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.

SimplePhysicsGame-Part2-PandaRigidbody

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:

SimplePhysicsGame-Part2-AllAnimals

 

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”.

SimplePhysicsGame-Part2-ObjectManagerCreation

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.

SimplePhysicsGame-Part2-LevelVariation

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

Introduction:

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.

Game

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.

SimplePhysicsGame-Part1-BackgroundSearch

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:

SimplePhysicsGame-Part1-Background




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

SimplePhysicsGame-Part1-Platforms

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.

SimplePhysicsGame-Part1-PlatformBoxCollider

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.

SimplePhysicsGame-Part1-PlatformsCollider




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.

SimplePhysicsGame-Part1-ScreenLimitInspector

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

SimplePhysicsGame-Part1-ScreenLimit

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:

SimplePhysicsGame-Part1-ScreenLimitCreation

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.