How to Build a Complete 2D Platformer in Unity

Would you like to build a Unity 2D platformer? Since the release of Unity 2017.1 in the summer of 2017, Unity Technologies has made making 2D games incredibly easy and fast.

In this tutorial, we will create a fully-featured 2D platformer. This project will incorporate a number of key topics including how to make cutscenes, how to quickly build and prototype a 2D level, and how to precisely choreograph game objects using the Timeline Editor.

This tutorial can also be thought of as the culmination of several tutorials – posted on Game Dev Academy – about these topics. You can check them out here:

Project Details

Let’s think about what we will be making. As said in the introduction, it will be a Unity 2D platformer. We will use the 2D character from the Unity Standard Assets pack. Then we will create an environment using tilemaps and Unity’s new Tilemap Editor. Then we will create an enemy that has an Idle/Attack animation (you’ll see). Then we will create a cutscene where the camera views the entire level then zooms on the character: here we will use the Timeline Editor and Cinemachine for 2D.

You can download the complete Unity project here.

Before jumping in, while not entirely required, it is good to have familiarity with Unity 2D and Cinemachine. Besides the tutorials above, you may also wish to check out some of our other Unity tutorials.

As we delve into the process of creating a complete Unity 2D platformer, it’s worth noting the value of Zenva’s Unity Game Development Mini-Degree. This comprehensive collection of courses allows learners to develop cross-platform games with Unity, exploring both 2D and 3D game construction as well as various in-demand skills like game mechanics, audio effects, procedural maps, and more. This Mini-Degree, with its focus on practical, project-based learning, serves as an excellent resource for those aspiring to master Unity and contribute to the game development industry.

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Tilemap Editor, Timeline, and Cinemachine

When Unity Technologies released the Unity 2017 version cycle, they introduced three pivotal tools designed to make Unity more accessible to artists. These tools were the Tilemap Editor, Timeline, and Cinemachine.

The Tilemap Editor, released later in the 2017 cycle, allowed users to “…literally paint directly in Unity” according to Rus Scammell, the project manager of Unity for 2D. The Tilemap Editor gives you the ability to create vast and complicated Tilemaps without having to use a third-party program.

The Timeline Editor and Cinemachine were released at the same time, though improvements to Cinemachine were released later. Cinemachine is a suite of cameras that allows you to create cutscenes, specify how the camera tracks a game object, and, in the end, allows you to tell a better story. Combine this with the Timeline Editor, a tool that allows you to choreograph game objects like a movie editor.

With these two tools, you can create stunning compositions without having to write any code.

Well, that’s a summary of the tools that we will be using! This tutorial is by no means exhaustive, for more information about these tools check out the tutorials linked above.

Setting up our project

The assets for this Unity 2D platformer project you can get here. Then create a new Unity project. Let’s import the 2D Standard Assets package by going to the Asset Store panel. Here, search for “Standard Assets” and download it.

img 5e58885786b8d

Then click on the Import button to import the asset. When the import window pops up, select the Standard Assets > 2D and StandardAssets > CrossPlatformInput folders only. Then import those.

img 5e588c28dcbf3

Next, we need to create two new folders. One called “Animations” and the other called “Tiles”.

img 5e588c9c23abf

In the Tiles folder, create another folder called “Tile Palettes”.

img 5e588cde54beb

The use of this folder will become apparent later on. Now let’s import Cinemachine by going to Package Manager (Window > Package Manager) and installing “Cinemachine”.

Snag 25823160

Now that we have everything imported we can start getting our tools in order. We will start with the Tilemap Editor. Go to Window > 2D > Tile Palette.

img 5e588ef514100

Place it in a sensible place on your workspace. I chose to put it in the space between the inspector and the scene view. Next, we need the Timeline Editor. Go to Window > Sequencing > Timeline.

img 5e58921502b03

The position of this tab is not set in stone so be prepared to move it around. We now have our workspace in order! Time to start creating our Unity 2D platformer!

Creating our environment

Go to your Tile Palette tab and create a new palette. Call it “Solids” since these are the tiles that the character will not be able to pass through.

img 5e5892f36e0ac

A palette works just like you would expect it to based on the name, it is a set of images that we use to “paint” with. Leave all of the settings set to default and save it in the Tile Palettes folder which we created in the Tiles folder.

img 5e58a3fab6c39

To set up our tiles, go to the Environment Tiles > Grass folder and drag those sprites into the Tile Palette window (do the same for any other tiles you want). Save the .asset files to the Tiles folder.

Snag 258001de

Now that all of our tiles are in order, let’s create a “canvas” to paint on. In your hierarchy, right-click and got to 2D Object > Tilemap.

img 5e5896577e1fb

What it has done is created a grid, and inside that grid is our “canvas”, also known as a tilemap. In order to start painting you need to familiarize yourself with the toolbar in the Tile Palette tab. With your rule tile selected, start experimenting with the various types of brushes. Once you feel pretty confident with the tools, start creating your Unity 2D platformer level! It can look however you want at this point.

Snag 254e603d

Adding the character

The last thing we have to do to our tilemap is to make it have physics interactions. Right now anything we put on it would just fall through the world – which as you can imagine is not ideal for a Unity 2D platformer.

To fix this, Unity Technologies released a new collider component called the Tilemap Collider. This behaves just like you would expect it to based on the title, it creates a collider around each tile. Go to your Tilemap and click Add Component.

img 5e589b1c78789

Search “CharacterRobotBody” and drag the character into the scene.

Snag 257f820f

We will be using the default character that came with the 2D standard asset pack. You can find the character by either going to Standard Assets -> 2D -> Prefabs and then dragging in the “CharacterRobotBoy”, or, you can just search “CharacterRobotBoy” and access it from there. Once the character is in the scene, you can hit play and move the character through the arrow keys. You may have to reposition the camera in order to see the robot.

Great! Now let’s add some challenge to our Unity 2D platformer.

Creating the enemy

In the “Enemies” folder from the asset pack, pick a certain enemy that you think would go well with your scene.

Snag 257f4e0e

Drag it into your scene and place it in a sensible spot.

Snag 257ef64a

Then create a new tag called “Enemy” and assign it to your enemy.

img 5e589caf9f3d7

img 5e589ce34c5f5

Now we are going to animate this enemy. You should already have the Animation tab open in the lower window of your workspace. If not, go to Window > Animation > Animation.

img 5e589d9a1b351

With your enemy selected, click “Create” in the Animation tab.

img 5e589e7000b34

Name it “Enemy1Controller”.

img 5e589ec3028e2

Save the animator controller in the Animations folder we created earlier.

Hit the record button and change the Sprite field to the other image that was provided (in my case it was called Saw_move, it may be similar if you chose a different enemy).

Then move about four frames ahead and change the image back to what it was before.

Then move a couple frames forward and change it back to the first image.

Snag 25727b29

Now if you hit play you will see that our enemy is animating! Cool!

Except, besides moving, our enemy doesn’t yet have a gameplay effect on our Unity 2D platformer.

Scripting our enemy

Let’s make it so that whenever the character touches the enemy the level restarts, which is pretty common for simpler Unity 2D platformers. The best way to do this is to actually create a new script and box collider on our robot character.

Snag 25751358

Set the box collider to Trigger and make sure it liberally covers the character. Name the new script “EnemyReaction”.

Snag 257e5f88

Let’s create a new folder called “Scripts” to house this component.

Snag 257e26f4

Here is the content of the EnemyReaction script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; // This is very important if we want to restart the level

public class EnemyReaction : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	// This function is called every time another collider overlaps the trigger collider
	void OnTriggerEnter2D (Collider2D other){
		// Checking if the overlapped collider is an enemy
		if (other.CompareTag ("Enemy")) {
			// This scene HAS TO BE IN THE BUILD SETTINGS!!!
			SceneManager.LoadScene ("scene1");
		}
	}
}

In order for this script to work, we need to do a couple of things. First, we need to have a collider on our enemy. Without this collider, our objects won’t know whether they hit each other or not (which kind of ruins the whole Unity 2D platformer thing).

Snag 257def3a

A simple box collider works best for covering all needs. Then, we need to save this scene as “scene1”.

Snag 25837a8a

Just save it in the root folder since we only have one, but if you are planning on having multiple scenes then you should create a new folder. Finally, that scene has to be put in the build settings. To do this, just go to File -> Build Settings

Snag 2584f8ec

and then click “Add Open Scenes”.

Snag 25862140

Now everything should work! Hit play and run into the enemy, the level restarts! Our Unity 2D platformer is closer to completion, but there’s a bit more to do.

Still in need of some foundations? Empower your journey in creating a Unity 2D platformer with Zenva’s Unity Game Development Mini-Degree, offering meticulous step-by-step guidance on building and optimizing cross-platform games with Unity, the engine driving over half of the world’s games.

Cinemachine and Timeline

We now come to the last part of this tutorial. In this part, we will be using Cinemachine and the Timeline editor. Let’s start with Cinemachine. You’ll notice that our camera isn’t following the character when he moves around. We can fix this by creating what is known as a Virtual Camera. Navigate to your toolbar and go to Cinemachine -> Create 2D Camera.

Snag 258772d6

Then assign the “follow” field on this camera to be the CharacterRobotBoy. Rename this to “PlayerCam”. Set the Aim to “Do Nothing” and set the Body to “Framing Transposer”.

Snag 258ab781

Next, let’s have a look at the Timeline Editor. Create a new, empty game object, named “Timeline”…

Snag 25941bfc

…and then click Create in the Timeline Editor. Call it “TheTimeline” and save it in the root folder.

Snag 25959dd9

You can have multiple timeline editors in a scene, which just adds to the complexity so we just have one. With this Timeline, we will create a cutscene where the camera views the entire level and then zooms in on the player. This will occur as soon as the scene starts. In order to do this, we need to combine Cinemachine and the Timeline Editor. We can do this through Cinemachine shot clips. In the Timeline, click “Add” and then go to Cinemachine.Timeline -> Cinemachine Track.

Snag 259730ae

Drag the Main Camera into the field.

Snag 2599686e

Then right-click and go to “Add Cinemachine Shot Clip”.

Snag 259a9805

With this track, we can specify how long we want a certain camera to be active. Let’s create a new 2D camera and position it so that we can see the entire level. Name this one “FullLevel”.

Snag 259db36e

This will be our first camera so select the Cinemachine shot clip and drag this camera into the “Virtual Camera” field.

Snag 25a15b77

Set how long you want this camera to last. One thing that helps is to set the timescale to be in second and not frames.

Snag 25a41e9e

Next, we need to create another clip for the player camera.

Snag 25a6d784

Place this at the end of the other clip. Drag either one of the clips on top of each other in order to smoothly transition. Now if you hit play, we have a pretty neat cutscene! But notice when the last track ends it goes back to the first camera instead of staying on the player. The easiest way to fix this is by deactivating the first camera after we have transitioned into the second. We can do this by using an Activation Track. As its title implies, the specified game object will stay active as long as the track persists. Create an Activation track…

Snag 25a80b70

…and drag the first camera into its field.

Snag 25a9c8c0

Then set the length to be a little after the transition. Now if you hit play, it all looks good and the player camera persists!

Unity 2D Platformer Outro

Congratulations on getting to the end of this tutorial. We now have a Unity 2D platformer in our hands, a perfect addition for any portfolio!

However, as I said before, this is not meant to be exhaustive. In order to find out more about these tools, you can either read the tutorials linked above, or you can explore them on your own! We also recommend you check out our web class on camera following and our mini-course on Unity 2D platformers as well, as they’re sure to help you cement the knowledge you’ve learned here!

However, expanding your knowledge is also important. So be sure to explore other useful Unity skills via other tutorials or courses.

For example, having a comprehensive foundation is essential. Zenva’s Unity Game Development Mini-Degree can provide exactly that: a thorough understanding of game development with Unity, including creating cross-platform games and working with diverse game genres. Whether aspiring to create the next big AAA title or simply enhance your gaming project, this Mini-Degree shares essential insights from basic coding to complex game mechanics, making it a valuable resource.

In either case:

Keep making great games!

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.