Skip to main content

Mario in Unity 6 (2025) | Part 2: Adding Player Movement

 

Mario in Unity 6 (2025) | Part 2: Adding Player Movement



Welcome to Part 2 of our Mario in Unity 6 series! In the last part, we set up our project and created the player and ground sprites. Now, it’s time to breathe life into Mario by implementing player movement with C#.

What’s Covered in Part 2:

  1. Writing a C# script to handle player movement.
  2. Assigning the script to the player object.
  3. Adding basic left, right, and jump mechanics.
  4. Fine-tuning the Rigidbody2D for smooth movement.

Let’s dive in!


Writing the Player Movement Script

We’ll start by creating a C# script to handle movement logic:

  1. In Unity, navigate to the Project Panel.
  2. Right-click and select Create > C# Script.
  3. Name the script PlayerMovement.
  4. Double-click the script to open it in your code editor (e.g., Visual Studio).

Paste the following code:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 7f;
    public float jumpForce = 12f;
    public LayerMask groundLayer;
    public Transform groundCheck;
    public float groundRadius = 0.7f;

    private Rigidbody2D rb;
    private bool isGrounded;
    private float horizontalInput;
    private bool facingRight = true;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, groundLayer);

        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            rb.velocity = Vector2.up * jumpForce;
        }

        Flip(horizontalInput);
    }

    // FixedUpdate is called once per frame
    void FixedUpdate()
    {
        rb.velocity = new Vector2(horizontalInput * speed, rb.velocity.y);
    }

    void Flip(float horizontal)
    {
        if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
        {
            facingRight = !facingRight;
            Vector3 theScale = transform.localScale;
            theScale.x *= -1;
            transform.localScale = theScale;
        }
    }
}

Assigning the Script to the Player

  1. Select the player sprite in the Hierarchy.
  2. Drag the PlayerMovement script from the Project Panel to the Inspector Panel of the player.
  3. In the Inspector, you’ll now see the public variables moveSpeed and jumpForce. Adjust these values to your preference:
    • moveSpeed: Set it to 5 for smooth movement.
    • jumpForce: Set it to 10 for a decent jump height.

Adjusting the Rigidbody2D Component

To ensure smooth physics-based movement:

  1. Select the player in the Hierarchy.
  2. In the Inspector, find the Rigidbody2D component.
  3. Set the following properties:
    • Gravity Scale: 2 (increases downward force for a more realistic jump).
    • Constraints > Freeze Rotation (Z): Check this box to prevent the player from rotating.

Testing the Movement

  1. Press the Play button in Unity to test the movement.
  2. Use the Arrow Keys or A/D to move left and right.
  3. Press the Spacebar to jump.

If everything is set up correctly, Mario should now move smoothly and jump when grounded. 🎉


What’s Next?

Now that our player character can move and jump, it's time to give life to Mario with an iconic look and smooth animations.

In Part 3, we’ll:

  • Import Mario's character sprite sheet.
  • Set up animations for running, jumping, and idle states.
  • Add logic to trigger animations based on player actions.

By the end of the next part, Mario will not only move but also look like the classic character we know and love, bringing a touch of nostalgia to our game.


Watch the Video:
Check out the 4-minute video for Part 2, where I demonstrate coding, assigning, and testing player movement in Unity 6.


Join the Series:
Follow this blog and subscribe to the YouTube channel for more updates. Let’s keep building our Mario game together!

#Unity6Tutorial #MarioGameDevelopment #PlayerMovement

Comments

Popular posts from this blog

How to Create a Timer or Stopwatch in Unity

How to Create a Timer or Stopwatch in Unity Timers and stopwatches are essential tools for many games, and Unity makes it easy to create them. In this tutorial, we will show you how to create a simple timer or stopwatch in Unity. Steps to follow: Create a new Unity project. Create a text object in the hierarchy. Create a new script called Timer and attach it to the text object. In the Timer script, create two public variables: timeRemaining and timeIsRunning . The timeRemaining variable will store the amount of time remaining on the timer, and the timeIsRunning variable will be a boolean that determines whether the timer is running or not. Create an Update function in the Timer script. In the Update function, check if the timeIsRunning variable is true and if the timeRemaining variable is greater than zero. If both of these conditions are true, subtract the time elapsed since the last frame from the timeRemaining variable. Create a displayTime function in the Timer...

Mario Game in Unity 6 (2025) | Part 3: Adding Animations (Idle, Run, Jump)

  Mario Game in Unity 6 (2025) | Part 3: Adding Animations (Idle, Run, Jump) Animations are what make a game truly come alive. In Part 3 of this series, we’ll add animations to Mario for idle, running, and jumping states using Unity 6. This step will transform our simple character movement into a polished and interactive experience. By the end of this tutorial, you’ll learn how to set up animations, configure the Animator Controller, and write C# scripts to handle animation transitions based on player input. What Are We Doing in Part 3? Importing already-sliced Mario sprites for animation. Creating animations for idle, running, and jumping. Setting up an Animator Controller. Writing scripts to control animation transitions dynamically. Step 1: Importing the Sprite Sheet 1.1. Import the Sprite Sheet Download File Download the pre-sliced Mario sprite sheet provided in this tutorial. Drag the sprite sheet into your Unity project’s Assets folder. Since the sprite s...