Skip to content

Tutorial

Pedro Esteves edited this page Nov 1, 2021 · 1 revision

Requirements

For starters, you need to install the Unity 2020.1.9f1 version and download the assets package.

Create Project

  • To create the project, open the Unity Hub, click on New Project and choose the 2D template.

Import Packages

  • After creating the project, go to Assets > Import Package > Custom Package... and import the assets package.

Develop the Game

Background

  • Change the MainCamera background color, by clicking the Camera object and setting the color to (56, 117, 159);
  • Create a Tilemap and using the Tile Palette, draw your level background. For more information about the Tilemap, go to the Unity docs.

Player

  • Drag the player sprite and drop it on the scene. Now you have a new object with a SpriteRenderer component, which contains the player sprite. The SpriteRenderer should have 1 in the Order in Layer field;
  • Add a Rigidbody2D, with a dynamic type. There are 3 types of objects: static (doesn't move), kinematic (moves with script), and dynamic (moves with script and physics);
  • Add a BoxCollider2D to the player. Now you should also add the TilemapCollider2D to the background so that the player won't fall forever;
  • Implement the player's jump: add the boolean canJump and call the function rb.AddForce();
  • Implement the horizontal movement;
  • Freeze the player's rotation on the z-axis. This can be done in the Rigidbody2D;
  • Enable the sprite flipping (change the flip boolean of the SpriteRenderer);
  • [Create the platforms (if you haven't done it before) and add to them a PlatformEffector2D to the platforms. The platform should be a prefab (drag one and drop it inside the Project Structure. Move the platforms into a new tilemap;]
  • Create an Animator for the Player with 3 states: jump, walk and idle. The 3 states should be managed from the player script.

Enemy

  • Duplicate the player object;
  • Create a new script called Enemy, and copy the player code;
  • Delete the code related to the player's input and make the enemy jump every time it touches the ground;
  • Create a prefab of the enemy;
  • [Go to the Player script and add collision detection with the enemy (OnCollisionEnter2D)]:
if player y position > enemy y position 
   kill enemy; 
else 
   kill player;

UI

  • Create a Game Over screen;
  • Create a GameManager / GameController: create a function to call the SceneManager's LoadScene() function. Add the current scene to the build settings. Every time the player dies and click the screen, the game restarts, by calling the previous function.

Coin

  • Drag and drop the coin into the scene;
  • Add a CircleCollider2D; change it to a trigger collider;
  • Create a coin animation (rotate the coin, for instance);
  • Open the exit door: OnTriggerEnter2D, call the SetActive() function of a new door object.

Door

  • When the player collides with the door, it should trigger the game-ending screen.

Build the Game

Now you should have something similar to what was developed during our workshop. To test your game outside the editor and distribute it, you need to build it. To do that, go to Build Settings, choose the platform you want to build for and click on Build.

Clone this wiki locally