Player Movement – 2D

2D Movement (Platformer with Jumping)

You don’t have access to this lesson
Please register or sign in to access the course content.
Length: 60 minutes|Difficulty: Standard

Demo

Introduction

This guide[2] covers how to make a 2D platformer movement using a Rigidbody2D and the physics system[3].

Make sure that you have the Input System[3] enabled before beginning.

You can download a prefab for the level and one for the platforms below.

Creating the Level

First create a number of platforms using Sprites. Add Box collider 2D components to them. Position them as a level layout.

This guide is incomplete.

The complete Player Movement Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;

public class PlayerMovement2DPlatformer : MonoBehaviour
{

    Rigidbody2D rb;
    bool isGrounded = false;
    Vector2 movement;
    int groundLayer;
    Transform playerGroundLocation;

    public float playerSpeed = 5;
    public float jumpForce = 1;


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        groundLayer = LayerMask.GetMask("Ground");
        playerGroundLocation = transform.Find("PlayerGround");
    }

    void OnMove(InputValue iv) {
        movement = iv.Get<Vector2>();
    }

    void GroundCheck()
    {
        RaycastHit2D hit;
        float distance = 0.5f;

        hit = Physics2D.Raycast(playerGroundLocation.position, Vector2.down, distance, groundLayer);

        if(hit.collider != null)
        {
            isGrounded = true;
        }
        else
        {
            isGrounded = false;
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        // Check if isGrounded
        GroundCheck();

        rb.velocity = rb.velocity + (new Vector2(movement.x, 0.0f) * playerSpeed * Time.deltaTime);

        // No horizontal movement, stop velocity if on the ground.
        if(movement.x == 0 && isGrounded == true) {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }

        // Can jump
        if(isGrounded && movement.y > 0) {
            //rb.velocity = rb.velocity + (new Vector2(0.0f, (movement.y * jumpForce)) * Time.deltaTime);
            //rb.AddForce(Vector2.up * jumpForce);
            rb.velocity = rb.velocity + Vector2.up * jumpForce;
            Debug.Log("IsGrounded - Jump: " + movement.y);
            isGrounded = false;
        }
    }

    void OnTriggerExit2D(Collider2D col) {
        if(col.CompareTag("Respawn") == true) {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }
}

You have now created the core features of a 2D platformer character[1]/player controller.

Terms definitions
1. character. Create a player object and controller that will move using the input system with jumping utilising a rigidbody and physics
2. guide. A user guide, document or tutorial to explain[4] how to complete a task.
3. system. A set of interconnected parts designed to transform, store, transport or control[5] materials, energy and/or information.
Terms definitions
1. character. Length: 60 minutes|Difficulty: Standard Demo Introduction This guide covers how to make a 2D platformer movement using a Rigidbody2D and the physics system. Make sure that you have the Input System enabled before beginning. You can download a prefab for the level and one for the platforms below. Creating the Level First create a number […]
2. guide. Length: 60 minutes|Difficulty: Standard Demo Introduction This guide covers how to make a 2D platformer movement using a Rigidbody2D and the physics system. Make sure that you have the Input System enabled before beginning. You can download a prefab for the level and one for the platforms below. Creating the Level First create a number […]
3. system. Length: 60 minutes|Difficulty: Standard Demo Introduction This guide covers how to make a 2D platformer movement using a Rigidbody2D and the physics system. Make sure that you have the Input System enabled before beginning. You can download a prefab for the level and one for the platforms below. Creating the Level First create a number […]
4. explain. Length: 60 minutes|Difficulty: Standard Demo Introduction This guide covers how to make a 2D platformer movement using a Rigidbody2D and the physics system. Make sure that you have the Input System enabled before beginning. You can download a prefab for the level and one for the platforms below. Creating the Level First create a number […]
5. control. Length: 60 minutes|Difficulty: Standard Demo Introduction This guide covers how to make a 2D platformer movement using a Rigidbody2D and the physics system. Make sure that you have the Input System enabled before beginning. You can download a prefab for the level and one for the platforms below. Creating the Level First create a number […]