Unity Tutorial – Basic 2D Platform Game (Part1)

Unity Tutorial - Part1 Title image

Disclaimer: I am a Unity3d beginner, so my way of doing things might not be the most efficient way. The purpose of this and future Unity3d tutorials is to document what I have learned and hopefully it will have some value for you as well. Let’s get started!

2D PLAtform Game Tutorial overview

  • Build the scene using Prefabs
  • Apply physics to the player (Rigidbody2D)
  • Write a C# script to move / jump with the player
  • Next Steps
  • Unity references

Build Scene using Prefabs

What is a prefab: A prefab is like a blueprint that can be used by other Game Objects. You create one prefab and use it for multiple instances that have the same properties.

All that is needed to create a prefab is to drag a game object, in this case a 2d Object -> Sprite -> Square, from the Hierarchy tab into the Project tab.

You can now drag multiple instances of that prefab square from the Project tab into the Scene in order to create the level you like.

Basic scene level using prefab squares

Since we want the player to land and move on these ground blocks, two more things need to be added.

  1. A Box Collider 2D component that can be found in the Inspector tab of the Cube prefab.
  2. A custom Tag (e.g. ground) so our player logic knows when we stand on the ground and when not (jumping).
Box Collider2D and custom tag added to prefab

physics (Rigidbody2D)

For the moment I use a simple 2D Object -> Sprites -> Capsule as the player.

Add a new GameObject Object -> Sprites -> Capsule

In order for the player to be able to stand, move and fall we need to add the following two components within the player (capsule) Inspector tab.

  • Rigidbody2d (In order to add physics so the player can fall)
  • Capsule Collider 2d (in order for the player to detect and stand on the ground)
Rigidbody2D and Capsule Collider2D components added to player.

If you press the start button now, the capsule should have gravity and fall (due to Rigidbody2d) and stop on the ground (due to box and capsule collider).


C# script

In order to control the movement and jump we need a C# script and attach it to the player. Create a script (I called it movement) and open the file in Visual Studio Code. Don’t forget to attach it to the player.

2d movement

There are a few different ways to create 2d movement to a GameObjects but in this Tutorial I use the transform.Translate function.

The code reads as follows:

  • Add a speed field that will allow to change the movement speed in the editor.
  • Capture the key press of the left and right arrow keys and store it into a variable, I called “movement”
  • Use the transform.Translate function from Unity3D and add “movement as the x value. Since we only want the player to move horizontally (left/right), the Y and Z fields can be Zero.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
    public float speed = 10f;
    void Update()
    {
        float movement = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
        transform.Translate(movement, 0, 0);
    }
}
Player movement for Horizontal Axis

Jump

The next thing to include to the same script is the functionality for the player to jump once the Space Bar is pressed.

The code reads as follows:

  • The previous move code has been stored in a custom function called move().
  • A custom jump() function has been created to add the jump logic via the velocity method.
  • A condition has been added in the Update function that will check if the “Space” bar is pressed and the player is on the ground. If so then call the jump method.
  • A Unity OnCollisionEnter2d method has been added to the player Object that will check if the player is touching the ground (tag on prefabs).
  • In the Update function the logic has been set so that the player can only jump if on the ground to prevent multiple jumps in the air.
using UnityEngine;
public class movement : MonoBehaviour
{
    public float speed = 10f;
    public float jumpForce = 12f;
    public bool isGrounded;
    Rigidbody2D rb;
   
    void Start() 
   {
       rb = GetComponent<Rigidbody2D>();
   }
    void Update()
    {
         move();
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            jump();
        }
    }
    void move()
    {
        float movement = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
        transform.Translate(movement, 0, 0);
    }
    void jump()
    {
        rb.velocity = Vector2.up * jumpForce;
        isGrounded = false;
    }
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Ground")
        {
            isGrounded = true;
        }
    }
} 
Player move and jump in Unity

Next Steps

You created a basic 2d movement and jump functionality for the 2d platform game, Congratulations!

Please check Part 2 of the series where I add a camera follow script and restrictions for the player to exit the scene.

Unity References

Links to the Unity documentation for the classes and methods I use in this tutorial.

2 thoughts on “Unity Tutorial – Basic 2D Platform Game (Part1)”

  1. Thanks, I’ve been trying to do this for so long, I watched YouTube tutorials tried lodes of diffrent websites but this is the only one which worked,
    I did have a small problem with jumping but I just needed to change a few thing within the script.

    Works very well
    Thanks for a grate tutorial 🙂

Leave a Reply

Your email address will not be published.