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.
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:
void OnTriggerEnter2D (Collider2D other) //the code below is only called when an object that includes a collider enters the trigger 2D collider attached to the screen limit object.
Destroy (other.gameObject); //Destroy the object that passes through the screen limit collider.
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.