Creating Simple 2D Player Movement and Jumping in Unity

In this tutorial, we'll walk through the process of setting up basic 2D player movement and jumping mechanics in Unity using a C# script. By the end of this tutorial, you'll have a player character that can move horizontally and jump when the spacebar is pressed.

In this tutorial, we’ll walk through the process of setting up basic 2D player movement and jumping mechanics in Unity using a C# script. By the end of this tutorial, you’ll have a player character that can move horizontally and jump when the spacebar is pressed.

https://youtu.be/Xn90Ge3LV8k

Step 1: Setting Up Your Unity Project

  1. Create a New Unity Project:
    • Open Unity Hub and create a new 2D project.
    • Set up your project settings as per your preferences.
  2. Set Up Your Scene:
    • Create a new GameObject for your player character (GameObject -> Create Empty).
    • Attach a Sprite Renderer component and choose a sprite for your player.
    • Add a Rigidbody2D and Collider 2D component to your player GameObject to enable physics interactions.

Step 2: Writing the PlayerController Script

  1. Create a C# Script:
    • Right-click in your Assets panel in Unity.
    • Choose Create -> C# Script and name it PlayerController.
  2. Edit the PlayerController Script:
    • Double-click the PlayerController script to open it in your preferred code editor.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float jumpForce = 10f;
    private bool isGrounded;
    private Rigidbody2D rb;

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

    void Update()
    {
        // Handle horizontal movement
        float moveInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);

        // Handle jumping
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        // Check if the player is on the ground
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }

    void OnCollisionExit2D(Collision2D collision)
    {
        // Check if the player is no longer on the ground
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }
}

Step 3: Implementing Basic Player Movement and Jumping

  1. Understanding the Script:
    • Variables: Adjust moveSpeed and jumpForce variables to tweak player movement and jumping physics.
    • Start(): Initialize the Rigidbody2D component.
    • Update(): Handles horizontal movement (Input.GetAxis("Horizontal")) and jumping (Input.GetButtonDown("Jump")).
    • Collisions: Detects whether the player is grounded using OnCollisionEnter2D() and OnCollisionExit2D().
  2. Attach the Script:
    • Drag and drop the PlayerController script onto your player GameObject in the Unity editor.

Step 4: Setting Up Your Scene

  1. Create Ground Objects:
    • Create one or more GameObjects with BoxCollider2D components.
    • Tag these GameObjects as “Ground” to ensure the player can detect when they are grounded.

Step 5: Testing Your Player Controller

  1. Play Your Game:
    • Press the Play button in Unity to test your player character.
    • Observe how the player moves left and right using arrow keys or A and D, and jumps using the spacebar.

Conclusion

Congratulations! You’ve now implemented basic 2D player movement and jumping mechanics in Unity. This setup forms the foundation for more complex gameplay mechanics, such as double jumping or variable jump heights. Experiment with different values for moveSpeed and jumpForce to achieve the desired gameplay feel. Happy game development!

Share your love