Every beginner in game development occasionally runs into the problem of how to get player to move and jump. There are several ways to move a player or object in Unity, but today we’ll show just one.


STEP 1 – Create a player

The very first thing we need to do is to create our player. Choose any 2D sprite that we drag and drop into our scene. We need to add Box Collider 2D and Rigidbody 2D components to the player to make gravity work on the player. We can’t forget to add Box Collider 2D to the floor as well.

Unity Editor
A sample of what it should look like

STEP 2 – Programming of player movement

Create the script “PlayerMovement” and open it in Visual Studio. First we need to declare the variable Rigidbody2D rb;. This variable needs to store the Rigidbody2D component from your player, so type rb = GetComponent(); in the Start() method to do so. Next, you need to declare the variable public float speed; to set your own speed.

And now the most important thing. The motion itself is created with rb.velocity = new Vector2(speed, rb.velocity.y);. In order to control the player, we need to put this command in a condition. Therefore, the whole code will look like this:


using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D rb;
    public float speed = 50;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.D))
        {
            rb.velocity = new Vector2(speed, rb.velocity.y);
        }
        else if (Input.GetKey(KeyCode.A))
        {
            rb.velocity = new Vector2(-speed, rb.velocity.y);
        }
        else
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }
    }
}

STEP 3 – Add a tag to the ground

In order to properly program the player to jump, we need to add a tag to the floor to distinguish it from other objects.

Click on your floor and in the tag box click on the “Add Tag” button.

Choose a name for your tag.

Check that your floor actually contains this tag.

STEP 4 – Programming of jump

To program jumping, we need to declare public float jumpForce = 20; and bool isGrounded;. To find out if the player is on the ground or not, we use the methods void OnCollisionEnter2D() and void OnCollisionExit2D(). To get the final result, we need to use rb.velocity = new Vector2(rb.velocity.x, jumpForce);, which allows the player to jump. This is what the final code looks like:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    Rigidbody2D rb;
    public float speed = 50;

    public float jumpForce = 20;
    public bool isGrounded;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.D))
        {
            rb.velocity = new Vector2(speed, rb.velocity.y);
        }
        else if (Input.GetKey(KeyCode.A))
        {
            rb.velocity = new Vector2(-speed, rb.velocity.y);
        }
        else
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }

        if (Input.GetKey(KeyCode.Space) && isGrounded == true)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }

    void OnCollisionExit2D(Collision2D collision)
    {
        isGrounded = false;
    }
}