Creating a 2D Platformer in Unity: The Beginning

Written by deniskondratev | Published 2024/04/08
Tech Story Tags: unity | gamedev | 2d-platformer | learn-gamedev | indie-gamedev | unity-course | gamedev-for-beginners | unity-tutorial

TLDRThis article introduces the first steps in creating a 2D platformer using Unity, aimed at those with a basic understanding of C# and the Unity interface. It guides through setting up the development environment, sourcing free game development assets, and initiating a Unity project. Key processes include setting up a development environment, creating a project in Unity Hub, downloading asset packs for game development, forming a Tile Palette for level design, and adding essential components like the Tilemap Collider 2D. This foundation sets the stage for more complex level development and introduces the upcoming focus on programming and physics for character movement control.via the TL;DR App

Welcome to the first part of our journey in creating a 2D platformer in Unity. This series of articles is designed to open the doors for you into the exciting world of game development, acquainting you in detail with the key features and capabilities of the Unity game engine. Our mission is to provide you with all the necessary knowledge and skills so that you can not only create your own games but also continuously improve your skills in this area.

This tutorial course will be useful for those who already have basic knowledge of C# and an understanding of how the Unity interface works. If you are not yet familiar with Unity, I recommend getting to know the editor through Explore the Unity Editor. For those wishing to deepen their knowledge in Unity programming, specialized courses on the basics are available on the Unity Learning platform.

The entire process described in this article can be seen in action by downloading the finished project from GitHub.

First Steps

How great it is to start something new and grand! But where to begin? To start creating a platformer, we need graphics, textures for the environment, and characters. Having artistic talent, in this case, is a great advantage, as your ideas will be able to come to life in the game. However, our course is aimed at programmers and technical designers, so we will rely on free resources available on the Internet—textures, animations, and sounds, which we will "bring to life" in our projects.

There are many resources for game development on the internet, both paid and free. One such resource is the Unity Asset Store, where you can find a huge number of materials. However, due to licensing restrictions, we cannot use even free assets from there in our educational materials.

Therefore, we will turn our attention to the website itch.io, where you can find resources available under the Creative Commons Zero v1.0 Universal license. For our platformer, an assets pack, Treasure Hunters from Pixel Frog, will be perfect. I would like to express huge gratitude to the author for providing the material, which is distributed completely free under the Creative Commons Zero v1.0 Universal license. Upon downloading, you will also have the opportunity to support the author with a donation. It seems our platformer will be imbued with the atmosphere of pirate adventures and treasure hunting. Arrr!!!

Setting Up the Development Environment

The first thing to do is to ensure Unity is installed on your computer. If not, start by downloading Unity Hub. After launching Unity Hub, a centralized application for managing different versions of Unity, go to the "Installs" section and click on "Install Editor.”

Select the Unity 2023.x version suitable for your operating system and wait for the installation process to finish. Next, an Integrated Development Environment (IDE) is required for writing and editing code. Unity is compatible with popular IDEs such as Visual Studio, Visual Studio Code, and JetBrains Rider. Visual Studio Code deserves special attention, being one of the most popular IDEs for Windows and macOS users. Don’t forget to install the Unity plugin from Microsoft to ensure the most convenient and efficient workflow.

If you need a more powerful tool, I recommend looking at JetBrains Rider. This is a paid program, but it is available for free for students for non-commercial use. Rider is especially valuable for those just starting their development journey.

Creating a Project

It’s time to bring the magic to life. In Unity Hub, go to the Projects section and click on the "New project" button.

Ensure that the Editor Version selected is the Unity 2023.x version you installed. Then choose the "2D Cross-Platform (URP)" template, and name our project "Treasure Hunters."

Now, let’s proceed to download the asset pack to create the environment and characters. In the game’s assets, create a folder named "Textures.” Do this by right-clicking in the "Assets" window, selecting "Create," and then "Folder,”

Into this newly created folder, we will upload and unpack the "Treasure Hunters" asset pack, which we mentioned earlier.

Creating Tile Palette

Now that we have everything we need, it's time to proceed to the next important step. Before we start creating the level of our treasure island, we need to form a Tile Palette from the downloaded sprites. It is with its help that we will be able to easily create the basis for our level.

First, create a folder named "Tile Palettes", and within it, create our first palette named "Palm Tree Island". To do this, as with creating a folder, right-click, then select "Create→2D→Tile Palette→Rectangular".

The next step is the correct slicing of the texture into sprites. Select the texture at Assets/Textures/Treasure Hunters/Palm Tree Island/Sprites/Terrain/Terrain (32x32).png. Since our textures are made as 32x32 pixel tiles, it will be optimal to set one unit equal to 32 pixels. In the Inspector window, set Pixels Per Unit to 32, and then click "Open Sprite Editor" to slice the texture into separate sprites.

In the opened Sprite Editor window, select the dropdown menu "Slice.”

In the "Type" section, select "Grid by Cell Size", enter the cell sizes 32x32 pixels and press "Slice".

After that, don't forget to press "Apply" in the top right corner of the Sprite Editor window.

Once the texture is ready, open the Tile Palette window through the main menu "Window→2D→Tile Palette". Drag the resulting texture into the opened window. Unity will suggest saving the resulting tiles. Do this in the same location as the palette itself. Eventually, you should have a window looking like this.

Drawing the Level

First, let's rename the scene from "SampleScene" to "Palm Tree Island.” This is the scene where we will create our game level. Add a tilemap to the scene by selecting "CreateObject → 2D Object → Tilemap → Rectangular".

Now is the perfect time to let your imagination run wild and create a small level. For now, our level may look quite modest, limited to just ground tiles, without a background and additional details. But don't worry, that's our current task. Here's what I came up with:

The last key element for our level is the collider. It's necessary so that characters, enemies, and items don't fall through the ground. Let's add a "Tilemap Collider 2D" component to the object on which we drew the level.

Conclusion

So, in this article, we've only scratched the surface of creating our platformer, laying down the initial groundwork for what will become a more complex and engaging level. If you have any questions or need clarification, I eagerly await your comments below this article. I am committed to responding to them, aiming to enhance your understanding of the material. Your feedback is invaluable, as it not only aids in improving your learning experience but also assists me in identifying and rectifying any oversights, thereby making the content even more transparent and approachable.

Remember, the focus of this course is primarily on the nuts and bolts of development rather than on level design. In the upcoming article, we'll plunge deeper into the programming and physics aspects essential for game development. We'll be concentrating on developing a character controller, an integral component that will breathe life into our platformer by allowing intricate and responsive character movement. This progression is crucial, as it will introduce you to the fundamental concepts that underpin game mechanics and the interactive elements that make games truly immersive.

Stay tuned for a deeper dive into Unity's powerful capabilities, where we'll explore how to infuse our game with dynamic interactions and a seamless gameplay experience. Your journey into game development is just beginning, and I'm here to guide you every step of the way. Let's unlock the full potential of your game development skills together.


Written by deniskondratev | Software Engineer / С++ / C# / Unity / Game Developer
Published by HackerNoon on 2024/04/08