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:
- Writing a C# script to handle player movement.
- Assigning the script to the player object.
- Adding basic left, right, and jump mechanics.
- 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:
- In Unity, navigate to the Project Panel.
- Right-click and select Create > C# Script.
- Name the script
PlayerMovement. - 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
- Select the player sprite in the Hierarchy.
- Drag the
PlayerMovementscript from the Project Panel to the Inspector Panel of the player. - In the Inspector, you’ll now see the public variables
moveSpeedandjumpForce. Adjust these values to your preference:moveSpeed: Set it to5for smooth movement.jumpForce: Set it to10for a decent jump height.
Adjusting the Rigidbody2D Component
To ensure smooth physics-based movement:
- Select the player in the Hierarchy.
- In the Inspector, find the Rigidbody2D component.
- 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.
- Gravity Scale:
Testing the Movement
- Press the Play button in Unity to test the movement.
- Use the Arrow Keys or A/D to move left and right.
- 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
.jpg)
Comments
Post a Comment