Lab 2- 2D Platformer

By Michael Shah | Last Updated 2/4/15

We are going to be making a 2D platforming game. The levels will be generated with a simple algorithm, and it will run from start to finish.

Building a 2D Platformer

Setting up the project

  1. Create a new Project, and set the defaults for a 2D Project.
  2. In the asset project(Cmd + 9 or Ctrl + 9), search for and download the 2D platformer package from the asset store from Unity Technologies.
  3. You may need to register and create a Unity3D account. Go ahead and do so now.
  4. Search for 2D Pack on the Unity Asset Store (Again from Unity Technologies as the publisher). Import the assets (Make sure to uncheck any of the project settings however, because that will mess up the settings we have created.).
  5. All we want is the coin sprite in this package. You can select that coin and import it.
  6. Save your scene (Call it “Level1” or something more creative if you like).

Creating the Hero

  1. Drag the Hero into the Hierarchy from the Prefabs/Characters.
  2. Remove any of the scripts that are attached to the hero
  3. The hero should have a RigidBody2D (with ‘Fixed Angle’ checked), Box Collider, and Circle Collider 2D.
  4. The animator will already have all of the animations set and rigged.
  5. Go ahead and delete the bazooka prefab, and the gun barrel.
  6. Set your hero Tag to “Player” if it is not already and hit ‘Apply’ so the changes effect all Prefabs.
  1. Make the Camera a Child of the hero by dragging the ‘Main Camera’ onto the ‘hero’. This will give us a third person view of our scene.
  2. Be sure to also zoom out the camera a bit in our scene (try a Z position of -20).
  3. You will also have to make sure the Projection is in Perspective, instead of Orthographic.
  4. Create a new folder called Scripts (If one already exists, then delete all of the scripts inside of it) and then a C# script called SimplePlatformController.cs in the folder.
  5. Add a function for FixedUpdate()
  6. Attach the script to the hero. (Add component->Script->SimplePlatformController.cs)
  7. Make sure to drag in from the hero the groundCheck GameObject into the Simple Platform Controllers Game Object Transform.

Creating Platforms

  1. Add a Quad to your scene. Rename it platform. Remove the mesh collider and add a Box Collider 2D, and a Rigid Body 2D. Change it’s layer to “Ground”
  2. Add a new Tag for the object so that it is tagged as “Platform”
  1. Check the mark for ‘isKinematic’ so it ignores gravity
  1. Make your platform a reasonable length. Save and test your game out!

Pro tip: Edit->Preferences->Colors->Play Mode Tint (Set it to something weird so you know you are in playmode. Remember, you cannot make changes that are saved in the game scene).

  1. Make your Platform a Prefab by dragging it into the Prefabs folder.
  2. (Optional) Create a material for your platform.

Spawning Platforms

  1. Now create an empty object called Spawner. We will now write a script for spawning random platforms.
  2. Write a Script called “SpawnPlatform.cs” and add it to your scripts.
  3. Add the component to your “spawner” Game Object.
  4. Drag your platform onto the “Platform” Game Object so it knows which object to instantiate.
  5. Move the Spawner somewhere ahead of your player.
  6. Save your scene

Falling Platforms

  1. Create another script called PlatformFall.cs and then add it to the ground.
  2. Attach both the “PlatformFall.cs to your Platform. Make sure to have at least one Platform underneath the player when the game starts.

Restart the Game

  1. Create a cube at a y position of -30(or somewhere far below your scene) so that it is way below our scene.
  2. Make the X Scale 10000, or otherwise a value very large that you think will cover your level.
  3. Remove the boxCollider
  4. Add a BoxCollider2D
  5. Make sure a check mark is next to ‘isTrigger’
  6. Make the Cube very long, so that it covers the screen.
  7. Rename it to ‘destroyObject’
  8. Turn off the mesh renderer by unchecking the box next to the component.
  9. Then add a C# script called “DestroyScript.cs”, so that the game restarts if we fall off the map we reload our scene and restart the game.

Coins

  1. Now lets add some coins that can be collected. Drag in the coinSprite into the Hierarchy from the Sprites folder.
  2. Write a script called “SpawnCoins.cs”
  3. Now lets write a “pickupcoin.cs” script that is applied to the coin.
  4. Add both scripts to the CoinSprite.
  5. Create three objects under our “Platform” Make them children of our Platform. Position them in three locations above the platform.
  6. Set the Coin Game Object to “CoinSprite”
  7. Drag the CoinSprite into the prefabs folder, and then you may delete it from our game scene
  8. Set the coins size to three and Element 0, 1, 2 to the coin spawns.

Other Edits to make on your own!

  1. Explore and make additions to your game to do the following!
  2. Change the background color
  3. Add some background music
  4. Add a score (Every time a coin gets picked up)
  5. Restart the level with different parameters for the platform spawn locations, and the length of the levels.