Skip to main content

Mario Game in Unity 6 (2025) | Part 4: Creating a Question Mark (?) Block

 

Creating a Question Mark Block in Unity 6 for a Mario Game



Creating a question mark block in Unity 6 for a Mario game can be an exciting and rewarding experience. This tutorial will guide you through the steps to achieve this, ensuring a seamless integration into your game.


Getting Started

Download and Import PNG Files


Download PNGs

Begin by downloading the necessary PNG files from the link provided above. Import these files into your Unity project to use as assets for the block and other components.

Organize Your Project

To maintain a clean structure:

  • Navigate to the Textures folder.
  • Create a new folder named PNG.
  • Import all the downloaded files into this folder.

Setting Up the Game World

Create Game World Object

  1. In the Unity editor, create an empty game object.
  2. Rename it to Game World.

Add Ground Object

  1. Drag the "ground" object into the Game World.
  2. Replace the default ground sprite with the actual floor sprite.
  3. Resize the ground to fit the scene.
  4. Reset the Box Collider 2D for accurate collision detection.

Creating the Question Mark Block

Create Question Mark Coin Object

  1. Inside Game World, create an empty game object.
  2. Name it Question_Mark/Coin.

Add Wall Sprite

  1. Add a square sprite inside Question_Mark/Coin and rename it to wall.
  2. Assign the question mark sprite to the wall object.
  3. Scale it up (set X and Y scales to 2).

Add Box Collider 2D

Add a Box Collider 2D to the wall object for collision detection.


Creating the Jump Section

Create Jump Section Object

  1. Inside wall, create an empty game object.
  2. Name it Jump Section.

Add Box Collider 2D to Jump Section

  1. Add a Box Collider 2D to Jump Section.
  2. Resize the collider to sit at the top of the wall.
  3. Change its layer from Default to Ground.

Adding Trigger for Animation

Add Another Box Collider 2D to Wall

  1. Go back to the wall object.
  2. Add another Box Collider 2D.
  3. Check Is Trigger.
  4. Resize it to cover the bottom of the wall. This serves as the hit trigger.

Creating and Implementing the Wall Collision Script

Create Wall Collision Script

  1. Create a new C# script named Wall Collision.
  2. Write the following script:
          
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class wallcollision : MonoBehaviour
{
    private Animator anim;
    public bool IsQuestionMark;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
       
    }
    void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.CompareTag("Player"))
        {
            anim.SetBool("isbounce",true);
            if(IsQuestionMark)
            {
                //TO DO...
               
            }
        }
    }
    void OnTriggerExit2D(Collider2D collision)
    {
        if(collision.gameObject.CompareTag("Player"))
        {
            anim.SetBool("isbounce",false);
        }
    }
   
}


Save and Attach the Script

Attach the Wall Collision script to the wall object.


Creating and Setting Up Animations

Create Animations

  1. With the wall selected, create a new folder in Animations for wall animations.
  2. Create two animation clips:
    • Idle: Set the initial position of the block.
    • Bounce: Move the block upward slightly and return it to its original position.

Configure Animator

  1. Open the Animator window.
  2. Add a transition from Idle to Bounce and back.
  3. Add a boolean parameter named isBounce.
  4. Set conditions for the transitions:
    • From Idle to Bounce, uncheck Has Exit Time and set isBounce to true.
    • From Bounce to Idle, leave Has Exit Time checked and set isBounce to false.
  5. Disable Loop Time for the Bounce animation.

Creating and Implementing the Question Mark Script

Create Question Mark Script

  1. Create a new C# script named Question Mark.
  2. Write the following script:
using UnityEngine;
using System.Collections;

public class questionmark : MonoBehaviour
{
    public Sprite[] backgrounds;
    private SpriteRenderer spriteRenderer;
    private int currentBackgroundIndex = 0;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        StartCoroutine(ChangeBackground());
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    IEnumerator ChangeBackground()
    {
        while(true)
        {
            spriteRenderer.sprite = backgrounds[currentBackgroundIndex];
            currentBackgroundIndex = (currentBackgroundIndex+1)%backgrounds.Length;

            if(currentBackgroundIndex ==0)
            {
                yield return new WaitForSeconds(.40f);
            }
            else{
                yield return new WaitForSeconds(.15f);
            }
        }
    }
}

Save and Attach the Script

  1. Attach the Question Mark script to the wall.
  2. Set the sprite array size to 3 in the Inspector and assign the sprites from the PNG folder.

Finalizing and Testing

Test Your Game

  1. Hit play and test your game.
  2. Verify that:
    • The background of the block blinks as expected.
    • The block bounces when the player hits it.

You’ve successfully created a question mark block with animations in Unity for your Mario game. For the complete script code, refer to the description.


Check out the step-by-step video tutorial for Part 4 on YouTube for a detailed walkthrough:

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 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: 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 ...

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...