How to make a 2D Game in Unity - YouTube

Channel: Brackeys

[0]
So, a question we get asked a lot is how to make a 2D game?
[3]
Of course, this is a huge question to answer.
[5]
But, hopefully, this video should help point you in the right direction.
[8]
And this is actually filmed on our brand-new video camera.
[11]
So, I hope the quality is all right.
[12]
Apparently, this camera has a pretty narrow lens.
[15]
So, we had to do this crazy setup to make it work.
[19]
Luckily, making 2D games in Unity has gotten so much easier over the last couple of years
[25]
Ever since they introduced the new 2D features in version 4.3
[28]
We've seen a bunch of cool features and workflows for different aspects of working with 2D in Unity
[34]
Now, 2d games, of course, come in a variety of different art styles and genres.
[39]
And the tools that are right for you are going to completely depend on the game that you're making.
[42]
But, in general, we can split Unity up into two primary workflows.
[46]
Tile based.
[47]
And sprite based.
[48]
The tile based workflow is meant for games that are build on a grid
[52]
As the name suggests
[52]
You create environments by placing tiles.
[55]
Which makes it pretty fast and easy to create and modify levels.
[59]
The sprite based workflow, on the other hand, gives you more control
[62]
As it simply means that you place object freely in the world
[66]
As a result of this you can create more detailed and dynamic environments.
[69]
But it might not be as quick.
[71]
And, on top of that, Unity is also working on some tools for vector graphics
[75]
In case you are really fond of illustrator.
[77]
But, regardless of what workflow you choose
[80]
We first have to create a project.
[82]
So, to create a new project, we go "new"
[84]
And your UI might look a tiny bit different here
[86]
But that's because I'm using Unity hub
[88]
Which is still in beta
[89]
But the idea should be the same
[91]
We then select a name for our project
[93]
I'm just gonna call mine "2D Game Example".
[96]
We then select a Unity version.
[98]
I'm gonna be using 2018.2
[100]
Then we chose a save location
[101]
I'll just use my projects folder
[103]
And, finally, as our template, we want to make sure that we select 2D.
[107]
Let's then hit create project
[109]
And Unity is going to open up a new empty project.
[114]
And, now, we can see we have this empty scene
[117]
With only a main camera in it.
[119]
And because we made sure to select 2d as the template
[122]
Unity has set up some different settings on our camera
[125]
And in our editor
[126]
To work well with 2D.
[128]
Awesome!
[129]
Now, when starting with an empty project
[130]
I really recommend visiting the Unity Asset Store to find some assets to use for your game.
[135]
Of course, if you want you can always replace them with your own custom assets later.
[139]
Luckily, there are some really great free assets available that we can use.
[143]
I've gone ahead and compiled a list of some of my favorites.
[146]
If you want to check them out you can use the link in the description.
[148]
In here we have a pack for putting together cool pixel art caves
[151]
They say picks up platformer pack
[154]
As well as some more toony assets
[156]
All these are based around tiles
[157]
And so will fit perfectly with the tile based workflow.
[160]
If you want to use the sprite based workflow
[162]
There's also an incredibly detailed art pack
[165]
As well as a top-down dungeon pack that would work great for that
[168]
Some of these also include characters
[169]
But I've gone ahead and added a few separate ones just in case.
[172]
Now, once you've picked something that you like
[174]
We have to get it into Unity
[176]
The way that we do this, it's by opening up the asset store window.
[179]
So, we'll go window
[180]
We'll go under "General" and select the "Asset Store".
[183]
You can also press "Control 9".
[185]
We'll then navigate to the package that we want to download.
[188]
I'm just gonna get the free platform game assets.
[191]
It's right here.
[192]
So we'll click on that.
[193]
And once you find the asset, you can simply click this red button to first download
[197]
And then import it.
[198]
We'll then get this window
[199]
Where we can see all of the different assets that are included in the pack
[203]
And if there's something in particular that you don't want
[206]
Let's say, you already have a character
[208]
And don't need this one
[209]
We can simply uncheck it,
[211]
But I'd like to import everything. So I'm just gonna a select everything and hit import.
[214]
This one is called BayatGames
[216]
And under here we have the free platform game assets
[219]
And you can see a bunch of different subfolders with the different aspects of our pack
[223]
And most packs will also come with some kind of a demo scene.
[226]
In this case, there's a demo folder right here.
[229]
If we open that up and double click on the scene to open it.
[232]
We then go into the scene view
[233]
And we can see this nice example of how you can combine the different elements to create a level.
[238]
You can, of course, select the 2D Rect Tool at the top here.
[241]
The shortcut for this is the T key
[243]
And if you also make sure that you're in 2D mode at the top of our scene view.
[247]
We can then go in and simply click and change different elements around.
[251]
We can, of course, move them.
[253]
Rotate them.
[254]
And scale them, as well.
[255]
And so you can quickly start modifying these levels to make them your own.
[259]
But, of course, we want to create our own custom level.
[261]
So we'll go back to the default sample scene
[264]
And we can now start dragging in assets to use them.
[267]
And how you go about this is going to depend on the pack that you're using.
[270]
This pack here splits up all the sprites into individual images.
[274]
So, if we're going to say "Tiles" and then "PNG"
[277]
We have some different sizes available here.
[278]
I'm gonna go under 256.
[280]
I'm gonna select the "grass platform" and drag it in.
[283]
And that works right away.
[285]
We have all of our different assets and each one of them is a separate image.
[289]
So, we can simply drag in some different images to sketch out a level.
[293]
But what most 2D packs do is that they used a tile set
[297]
And a tile set is basically just a very large image
[300]
With a bunch of sprites laid side by side
[303]
For example, if we go to the asset store and download the sunny land
[308]
And open up the folder here
[309]
We can see that on the artwork
[312]
And then environment
[313]
In here, we can see that there is indeed a file called tile set
[317]
And if we have a look at this
[318]
We can see that it's basically just a bunch of smaller sprites in one larger image.
[323]
Now, if you want to learn how to create your own tile sets
[325]
And set them up in Unity.
[326]
We have a tutorial that I suggest you check out
[329]
But, luckily, this package has done all that for us.
[332]
If we just select the tile set sliced here
[334]
We can see that they've actually gone in and marked all the different tiles that make up this tile set
[340]
And if we expand this arrow here
[342]
We can actually see each individual tile in our tile set.
[346]
Pretty cool!
[346]
And just like any other image we can take any one of these.
[349]
And simply drag them in to the editor.
[352]
Now, this one is a lot smaller, but you can see it works just fine.
[355]
So, now that we know how to work with sprites.
[358]
Let's try and build out a quick level here.
[360]
So, we already have this grass platform here.
[362]
That's great. I'm just gonna place it here.
[364]
Let's also add a background.
[365]
So, I'm gonna go under new background.
[368]
Under PNG.
[369]
I'm just gonna select the lowest version here
[371]
Under background
[372]
And here we have another file.
[374]
Let's simply drag that in, as well.
[376]
And we can see this looks nice in our camera.
[378]
But we can also see that it's overlaid on top of a grass.
[382]
And, of course, we want our background to be behind it.
[386]
For this we use something called sorting layers
[388]
So, to the right in our inspector here
[390]
Under the Sprite Renderer
[391]
We can see the setting called the sorting layer
[393]
If we click this, we can add a new sorting layer.
[396]
This takes us to another menu
[398]
Where we can hit the plus sign to create a new layer
[400]
Let's call this one "background"
[402]
And if we want this to be behind the default layer
[405]
We actually need to put it on top
[407]
That's because this actually represents the drawing order
[411]
In other ways, we first draw our background
[413]
And then we draw the default layer on top
[416]
Let's just go in and add another one here called the "foreground"
[419]
In case you want to have some foreground elements as well.
[422]
So, now we can go into a new background
[424]
And we can change the sorting layer here to background.
[427]
And "voil脿"!
[428]
A grass platform appears.
[430]
We can also maybe add a coin to our game
[433]
To do that we'll go under the coin animation.
[435]
Let's go into PNG again
[436]
And let's just select the small version here
[438]
And as you can see this has a bunch of different images
[441]
And if we go through these quickly
[443]
We can see that they make up an animation.
[446]
So, to add these as a single animated object.
[449]
We simply select the first one
[450]
Hold down shift
[451]
And select the last one
[453]
And we then drag all of them in
[455]
Unity is going to notice that we are dragging in a bunch of images
[458]
And so it's gonna ask us to create a new animation with these images.
[462]
I'm gonna make a new folder for this
[464]
I'm gonna store all of our animation in here.
[467]
Let's open it up.
[468]
And let's call this the "coin animation".
[471]
Let's hit save
[472]
And inside of our animation folder
[474]
Unity is now going to create both a animation file
[477]
As well as an animator controller
[479]
And this is currently named after one of our images.
[482]
I'm just gonna rename this to "coin animator".
[485]
And now if we select our coin
[487]
We can see that it has an animator component as well
[490]
If we go to the animation window
[492]
Which you can always find on the window
[494]
Animation
[495]
We can actually go through each individual image in this animation
[499]
If we hit play
[500]
We can play through it
[502]
Or if we simply hit this button to play the game
[504]
We can see that the animation plays and repeats by default.
[508]
So, now we have an animated coin in our game
[512]
We can, of course, move this around
[514]
Put it anywhere that we would like
[516]
And just like when creating any game in unity
[517]
We can then add components to our different game objects in order to make them do different things
[523]
Say, for example, that we wanted this coin to have physics applied
[526]
Well, then we could go ahead and add a new component.
[529]
We can go under physics 2D
[531]
And here, we have a bunch of different components for adjusting 2D physics on our game object.
[536]
I'm just gonna select the Box Collider 2D
[539]
If we then go into the Scene view and select our coin and hit F to focus on it
[544]
We should be able to seeing that there's actually a green line surrounding our coin.
[548]
It might not be too visible from the screen recording, but it is there.
[552]
I'm just gonna give it a size of 1 on the X
[555]
This way we can kind of see it more clearly
[557]
And the Y is just fine as it is.
[560]
But adding a Collider is not enough.
[562]
If we want this to fall onto our platform
[564]
We also have to apply physics to it
[567]
To do this, we'll go add component
[568]
And we'll search for rigidbody 2D
[571]
This will apply it physics to our coin
[573]
And if we now hit play
[575]
We should see that immediately our coin falls.
[578]
However, because we don't have a collider on our grass,
[582]
It's just gonna fall right through on platform
[584]
So, that's here go and add a box Collider 2D.
[588]
And we can go in and edit the collider here
[591]
And I'm just gonna drag the top a bit down
[594]
So, that it looks like our coin is landing at the center of our platform
[598]
I'm also going to take the platform and drag it down a bit
[601]
And if we now hit play once more
[603]
We can see that the coin falls and lands on a platform
[606]
We also see that we have this weird issue
[608]
Where sometimes our coin is behind a platform
[611]
And sometimes it's in front
[613]
And that's because they're currently on the same sorting layer
[616]
And not only that they have the same order within that layer
[619]
To change that we can go to our coin here
[621]
And we can increase the order in layer
[624]
So, if we set this to any value greater than 1
[626]
It will be drawn on top of our platform
[628]
I'm just gonna set it to something like 10
[630]
And now when we hit play
[632]
Our coin always appears on top of our platform.
[635]
So, that is kind of the very basics of working with 2D objects in Unity,
[639]
And currently we're just using the default sprite workflow
[642]
We can take any of these objects and drag it around freely.
[645]
In other words, we aren't restricted to a grid.
[648]
However, because this platform actually has a bunch of different tasks in it.
[652]
It would work great with the tile based workflow.
[655]
So, let me just give you a quick preview of how that would work.
[658]
If you want to learn more about using Tile Maps in Unity,
[660]
We have a tutorial dedicated to that. So definitely check that out.
[663]
So, for now I'm just gonna take these two objects and remove them
[666]
And instead I'm gonna right-click in our scene and I'm gonna go to the object
[670]
And create a new tile map
[672]
This creates an object in our scene called grid
[674]
And as you can see this does overlay a grid on top of our scene.
[678]
I'm gonna set the cell size of this grid to 2 by 2
[681]
This way it's a bit larger
[683]
And under this grid, we have the tile map.
[686]
We can also go and open up the tile set window
[689]
So we'll go window
[690]
2D
[691]
And select the Tile Palette
[693]
Let's dock this to the side of our Scene view
[696]
And the tile palette is basically just the window where we add all of our tiles
[700]
And then we can select between them in order to paint them into our scene.
[704]
So, I'm gonna create a new palette here
[706]
Let's just name it something like "Grass Platform palette".
[711]
I'm gonna hit create
[712]
And I'm just gonna store it in the base folder here
[714]
So, it's select folder
[715]
And now we can simply drag sprites in here in order to add them to our palette
[719]
Of course, we have a bunch of tiles right here
[721]
So, I'm just gonna select all of them and drag them right in.
[725]
And it's going to ask us where we want to generate all the tile assets
[728]
I'm gonna right click and create a new folder for this.
[730]
Let's call it tiles
[732]
And it's it that folder
[734]
It's then gonna generate all of our tiles
[736]
And voil脿!
[736]
We can now see all of our tiles inside of this window
[739]
And we can select any one of them by simply clicking on it
[742]
And by selecting the brush tool at the top here
[745]
We can then start painting them into a scene.
[747]
We can also see that they don't quite line up with our grid
[751]
To change that, we need to select all of our sprites.
[753]
And to the right here, we'll change the pixels per unit
[756]
And each of these sprites is 256 by 256
[760]
And we want each one of them to be two units wide
[763]
Because we set our grid to 2 by 2
[766]
And so we simply need to take this value and half it
[769]
And that makes 128.
[771]
So, we'll set the pixels per unit to 128.
[774]
And if we then apply that
[775]
We can see that they snap right into place
[777]
So I'm just going to use the paint brush and erase the two two a quick level here
[784]
Awesome!
[785]
Again, I really suggest you check out our tutorial on tile maps
[787]
If this is the workflow that you want to use.
[790]
So, hopefully, that should give you an idea of what tools you have at your disposal
[794]
When starting to make 2D games in Unity.
[796]
Of course, this is only the beginning
[798]
And we'll be releasing new videos on the different aspects of making 2D games going forward
[802]
Starting with player movement
[804]
If there are any particular subjects that you would like to see
[806]
Like enemies, one-way platforms or shooting
[810]
Definitely let us know in the comments
[812]
Until then I recommend just playing around with Unity
[814]
And having some fun sketching out your own levels
[817]
Also if you like this video and the content in general
[819]
Consider supporting us on Patreon
[821]
Patreon allows you to make a monthly donation with an amount of your choosing
[825]
You can cancel it at any time
[827]
And it's just a great way to help us keep making these videos
[830]
On that, thanks for watching!
[831]
And I will see you in the next video!
[833]
Thanks of the awesome Patreon supporters who donated in June
[836]
And especially thanks to...
[865]
You, guys, rock!