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