6 Creating a basic 2D platformer


This chapter covers

  • Moving sprites around continuously
  • Playing sprite-sheet animation
  • 2D physics (collision, gravity)
  • Camera control for side-scrolling games

Let’s create a new game and continue learning about Unity’s 2D functionality. Chapter 5 covered the fundamental concepts, so this chapter will build on those to create a more elaborate game. Specifically, you are going to build the core functionality of a 2D platform game. Also called a platformer, it’s a common type of 2D action game, best known for classics like Super Mario Brothers: a character viewed from the side runs and jumps on platforms, and the view scrolls around to follow. Figure 6.1 shows what the end result will be.

Figure 6.1 The final product of this chapter


This project will teach concepts like moving the player left and right, playing the sprite’s animation, and adding the ability to jump. We’ll also go over several special features common in platform games, like one-way floors and moving platforms. Going from this shell to a full game mostly means repeating those concepts over and over.

6.1 Setting up the graphics

6.1.1 Placing the walls and floor

6.1.2 Importing sprite sheets

6.2 Moving the player left and right

6.2.1 Writing keyboard controls

6.2.2 Colliding with the walls

6.3 Playing the sprite’s animation

6.3.1 Explaining the Mecanim animation system

6.3.2 Triggering animations from code

6.4 Adding the ability to jump

6.4.1 Falling from gravity

6.4.2 Applying an upward impulse

6.4.3 Detecting the ground

6.5 Additional features for a platform game

6.5.1 Unusual floors: slopes and one-way platforms

6.5.2 Implementing moving platforms

6.5.3 Camera control