Creating a 2D Platformer on Unity from Scratch

What types of games do you like most: mazes, obstacle courses or something else? Today we will tell you how to make a 2D-platformer in Unity – this is a genre in which you have to travel through levels. Playing is very exciting, and developing is even more interesting – let’s show you how to create such a game in Unity for young programmers.

Before you start creating a 2D-platformer on Unity, start the program and create a new 2D project. Assign a name to it and click Create Project.

You can draw sprites for the game yourself or find ready-made ones on the Internet. We will need sprites of the character, platforms and background, download them and import them into our project.

With the help of the sprite of the ground we will collect one large platform. For convenience, let’s move the game window next to the scene window. Using the CTRL+D key combination, we duplicate the ground block. If you hold down CTRL key while dragging, the movement will be made on an invisible grid – we duplicate it until we make the necessary platform. Create an empty object and place all blocks in it, now the platform can be moved and scaled as a whole.

Create a Canvas (canvas) on which we will place the background. The scale of the canvas will be set by the screen size, for this purpose set the resolution manually.

For the background, import the textures that were found on the Internet. Next, set the texture you like as the background and choose a color for it. Now let’s set the image type. The parameter Tiled will evenly cover the specified texture all the space, not stretching it, but exactly repeating it. The position of the texture will be set across the entire width and height. Select this parameter with the Alt key pressed. Let’s change the color a little bit. Set the camera for the canvas and thus move all the game objects to the foreground.

Go to the folder with sprites of the character. Take the first frame from the IDLE animation and move it to the stage. Zoom in on the character and remove the blur from all of his animations. This should be done on all pixel sprites.

Adding a Box Collider to a 2D platform in Unity

Let’s create a folder for animations and an animator in it to build the transition logic between animations.

Now make the player itself and move the sprite into it.

Open the animation window and move to a convenient place. Create an animation IDLE and save to the previously created folder for animations. Select all frames and move.

The speed of the animation can be changed in 2 ways:

  • stretch it in this window;
  • Change the speed value in the animator.

Let’s open the animator. Select the player sprite, and in the animator animation IDLE. Here you can set the speed of the animation playback. Run the game and check. When the game is on, you can select the optimal speed.

Check the transition between rest and run animation

Next, make a reverse transition and uncheck Has Exit Time for each transition. Set the transition duration to 0. These parameters are necessary for a smooth transition between animations, which in this case is not required.

Now create a boolean variable isMoving, which will switch the character from the state of rest to movement and back. The condition for the transition here will be isMoving is true. The transition from running to rest isMoving is false. Let’s run and test the animation switching.

For the player let’s make a capsule 2D-Collider with size customization. Here it is important not to confuse it with a regular one, which is intended for 3D-objects.

Writing scripts in Unity

  • Let’s create a separate folder for this purpose. Designate the script.
  • Remove comments.

Add a variable to specify the speed of the player’s movement and a vector that specifies the direction of movement, as well as a reference to the rigidbody component. In the start function, assign this component to the variable. The start function is executed once when the game starts, unlike the Update function, which is called every frame of the game. In this function, we will call the Move function, which is responsible for character movement.

The Move function will start by getting the direction of movement from the keyboard. Horizontal means that we read the keys a and d, and then according to this we store in Input a number from -1 to 1. The longer we press the key a, the closer the number gets to -1, respectively, and the character moves faster to the left side. With the right side is the same, but you need to press the d key.

Then to the coordinates of the player add the movement, which is calculated by the formula: direction multiplied by speed and by the time between game frames. Speed multiplied by time is the distance that the character will travel in the direction of the input vector.

Let’s create a reference to the SpriteRenderer component. To rotate our character along the direction of motion, let’s add a condition to Move. If the direction is not equal to zero, i.e. we move the character, we rotate the sprite in the corresponding direction. The nested condition is exactly what we need to activate the rotation if we turn the character.

Let’s make Sprite Renderer a public variable to put a reference to the character’s Sprite Renderer in it. We move the sprite into this field. Run it and test it. The rotation works.

Create a script in Unity to control the switching of animations

Let’s create 2 variables:

  • A variable that will store a reference to the animator;
  • a variable that is responsible for switching the running animation on and off.

In the start function, save the animator component to a variable. In the update function we will synchronize the value of the isMoving variable with the same variable from the animator.

In the character movement script we will also create such a variable and additionally an object of the character animation script class. In the Start function we will save the component of the animation change script into a variable, and in the Move function we will switch the state of the isMove variable in the corresponding condition:

  • if the direction is not equal to zero, then there is movement;
  • if the value of direction is zero, there is no change of position.

At the end we send the value of the movement variable to the animation change script, which in turn will change the variable in the animator.

Run it and test it

Other animations are added to the game in a similar way: the more game mechanics there are, the more complex the logic to implement them will be.

We have finished creating a 2D platformer on Unity. Now you can experiment on your own, for example, change the character, environment or try different speed of transitions.

robert-m-bailey