Lab 2

Building a 2D Game – Infinite Runner

For this project we are building what is known as an ‘infinite runner’. That means that the game has no finite length, it is constantly updating and changing as the player plays.

This means that we will be algorithmically generating the levels and contents of the game.

Obtain Assets

  1. Go to the asset store (Cmd 9, Control 9, or Window->Asset Store). In the search box, type in “Sample Assets” and download them to a location you can access.
  2. We really only need a few of the assets. ‘uncheck project settings’. It is okay to download everything else.
  3. Add the assets to your Unity Project. Save your scene
  4. Add a folder called Prefabs, Scripts, and Scenes into your project.

Build a Level

  1. We are going to create three pieces of ground.

Tip: Create them in order of largest to smallest, and then just delete sections of them.

  1. (small) One small sprite piece of ground that is slippery (0 friction, 0 resistance)
  2. (medium) One that is two in length
  3. (large) One that is three in length
  1. Once you have created your prefabs, create a large piece of ground for the player to start from.
  2. Now go into the 2D assets that we have imported.
  3. Select the 2D Character (Sample Assets->2D->Prefabs->2D Character) and place him over the ground.
  4. The player will already have a 2D Character Controller that has been built, so he can run around and jump.
  5. We then want to make our character slippery, so we will add to the characters Box Collider 2D a Material for slippery (Sample Assets->2D->Physics materials->Slippery).
  6. Next we will modify the Platform2DUser Control.cs script. So go ahead and click on this (Which is found in the inspector of our Character).
  7. We are going to remove the ability to allow the player to crouch, and move left and right. We want them to only be able to jump.
  8. See the final script below.

  1. Next, we will want the ability to Double-jump. We will do this in PlatformCharacter2D.cs.
  2. Add a field: booldoubleJump = false;
  3. Modify FixedUpdate() and at at the bottom.

if(grounded)

doubleJump==false;

  1. In the Move Function update the bottom block, so that.

if((grounded || !doubleJump) & jump){

anim.SetBool(“Ground”,false);

// 0 out our y velocity

rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);

rigidBody2D.AddForce(new Vector2(0f,jumpForce);

// If we’re not on the grounded, then able to jump.

if(!grounded)

doubleJump=true;

}

Add a Scrolling Background

  1. Select your Main Camera, and change the Background color.
  2. Go on youtube Mike Geig and find tutorial

Manipulating the Camera

  1. Create a Scripts Folder for your project.
  2. Create a C-Sharp Script called CameraRunnerScript.cs.

Click and Drag your 2D Character (Your player) onto the Camera’s Player, so that they follow.

Ending the Game

  1. We’re going to build blocks behind and under the player so that if the player runs into them, the game will restart (i.e. the player fell off the platform and must restart).
  2. Add a new GameObject for a Quad. Attach the Quad to the Main Camera, so that it is a child of it. Then position it behind the player, and scale it so that it is a long vertical bar.
  3. Remove the mesh collider.
  4. Add a Box Collider, make it a trigger.
  5. Add a RigidBody2D Collision.
  6. We will now create a new C-Sharp Script called DestroyerScript.cs
  7. Now drag the script onto the Vertical bar we have created.
  8. Now duplicate our Destroyer, and scale it so that it is a long horizontal bar.

Building the World (Adding Platforms)

  1. Create a new prefab called “Ground Spawn”
  2. Create a new quad, and attach to it the “Ground Spawn”
  3. Create a new c-sharp script called “SpawnScript.cs”
  4. Then add in the Obj your 3 platforms.
  5. Delete the Quad we created.
  6. Drag back into the editor, the Ground Spawn Prefab, and put it somewhere beyond our original platform to the right. Make sure that it is nested under the camera. Repeat this process once again. Repeat this process again. Make sure your quads are outside of the range of the camera, so they are not seen.

Adding Game Elements – Heads Up Display for Score

  1. Add a new script for a HUD (Heads Up Display) called HUDScript.cs.
  2. Click and drag the HUD script over the Main Camera.

Adding Game Elements – Adding Powerups

  1. (Sample Assets->2D->Sprites->prototypes_2) “Add in the Green Block”.
  2. Add a BoxCollider2D to it. Make it a trigger.
  3. Create a new script called “PowerUpScript.cs”
  4. This next script is a bit inefficient, but it is simple and sufficient for a prototype.
  5. Create a PowerUpSpawner prefab.
  6. Then also build a quad from our powerups. Add to it the spawn script.
  7. For the quad, only put an object(In the inspector under Obj” to be that of our Powerup block that we just created. Set the SpawnMin and SpawnMax to 2 and 5. Now drop our quad into the prefab list.
  8. Create a prefab, and put it under the camera. And put two PowerUp spawns to be on the top and bottom rows. Make the power up that appears on the bottom appear more frequently, as it is much harder to get, and we want to reward our players.

Add a Game Over Scene.

  1. Modify our DestoryerScript.cs
  2. Either send in a ‘1’ or the name of the scene.
  3. Update your HUDScript so that when we transition from a new scene, the score gets stored in the Player Preferences. (You could otherwise use a packet that persists throughout your game for security purposes)

Now save our scene, and make our new scene, lets save it as “GameOverScene”

  1. Save our scene.
  2. Create a new script called “GameOverScript.cs”