Skip to content

Tutorial

Pedro Esteves edited this page Nov 11, 2021 · 4 revisions

Tutorial

Requirements

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

Import Packages

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

Develop the Game

Background

  • Create a canvas
  • Put the background image on canvas and stretch it
  • Change the Canvas Render Mode into World Space
  • Set the background position to (0, 0, 5), and the canvas scale to (0.026, 0.026, 1)

Player:

  • Drag the player sprite and drop it on the scene
  • Scale it to (3, 3, 3)
  • Position it on the beginning of the level
  • Add a RigidBody2D with a dynamic body type
  • [Add the floor: an empty GameObject with a BoxCollider2D]
  • Add a BoxCollider2D
  • Create the Player script
    • Move to the right: Input.GetButton() + Input.GetAxis() + Flip Sprite + GetComponent()
    • Implement the jump: GetComponent() + rigidbody.AddForce()
      • Create the variable isJumping, the method OnCollisionEnter2D()
      • Create and add the tag Floor to the Floor object
  • Change Order in Layer
  • Freeze Rotation
  • Add an Animator to the Player: create 3 states (idle, walk, jump), and 2 parameters (walking and jumping)
  • Set the parameters by code

Platforms

  • Drag the sprite and drop it on the scene
  • Add a BoxCollider2D
  • Add a PlatformEffector2D, and tick "Used by effector" on the BoxCollider2D
  • Create the platform Prefab

Walls

  • Add walls to prevent character from exiting the game boundaries

Collectibles

  • Drag the sprite and drop it on the scene
  • Create a BoxCollider2D, with a trigger
  • Create and add the tag Collectible to the object
  • Create the collectible Prefab
  • [Go to the Player script
    • Create an OnTriggerEnter2D() method, and destroy the collectible inside it
    • Get the collectibles AudioSource and call the Play method ]
  • Animations:
    • If you drag the sprite sheet and drop it on your scene, Unity will try to create an animation automatically
    • You can use that to create the Animation and Animator of the platforms
    • Now, add the Animator component to you platform prefab and move your new Animator to that component
    • This way, you can create the animations for all the platforms on your scene
    • (Don't forget to delete the new object after completing this process)

Collectibles AudioSource

  • Create an AudioSource with the collectibles sound

Collectibles UI

  • Add an Image with the chip sprite
  • Import TextMeshPro and add text to the canvas

AI

  • Duplicate the Player object and the Player script
  • Rename both to Enemy (don't forget to change both the script and the class names)
  • Create the movementTime variable
  • Change the Update method to:
currentMovementTime += Time.deltaTime;
if(currentMovementTime >= movementTime)
{
    goingRight = !goingRight;
    currentMovementTime = movementTime - currentMovementTime;
}

transform.position += new Vector3(speed * Time.deltaTime * (goingRight ? 1 : -1), 0, 0);
_spr.flipX = !goingRight;
  • You can also add an Animator and a walking animation to the enemies

Improve the collisions with platforms

  • On the Update method:
if (isJumping && Physics2D.OverlapBox(((Vector2)transform.position + Vector2.down * .75f, Vector2.one * .25f, 360, layerMask) && _rb.velocity.y < 0) {
    isJumping = false;
}
  • Draw the box using the DrawGizmos() funcion. This will help you debugging the previous code.

Small Jumps

if(isJumping && Input.GetButtonUp("Vertical") && _rb.velocity.y > 0) {
   _rb.velocity = new Vector2(_rb.velocity.x, _rb.velocity.y * .5f);
}

Jump Buffer

if(Input.GetButtonDown("Vertical"))
{
   jumpBufferCount = jumpBufferLength;
} 
else
{
   jumpBufferCount -= Time.deltaTime;
}

// Jump Condition
if (jumpBufferCount >= 0 && !isJumping) { ... } // Jump code

Build the Game

  • To build the game, you need to go to File > Build Settings
  • Select your current scene, and click on Add Open Scenes, if your scene isn't on the "Scenes in Build" block
  • Choose the target platform (PC, Mac & Linux Standalone)
  • Click on Build (or Build And Run)
  • Now you can play the game whenever you want

How to learn more about Unity