2D Mario Clone Game Tutorial – Unity3D (C#)


2D Mario Game Tutorial – Unity3D (C#)

mario

 

Part 2

Play Demo

We will create one of the most famous game character in this tutorial: MARIO! I guess everybody knows this plumber guy’s story. He survives against the monsters for the meet princess. In this tutorial, I will show you how to create animations and little examples for some funtions of game.

Before the begining, please download the sprite sheet here or get your own from google. We need this sprite sheet to create animations and some objects.

Lets start with Mario character. CreateScreen Shot 2015-03-16 at 13.13.51 a Sprites folder and import sprite sheet here. Select sprite and  make the Sprite Mode Multiple. If you get another sprite, Pixels Per Unit value may be different but it’s 32 for my sprite. Open sprite editor.

 

 

 

Screen Shot 2015-03-16 at 13.18.05

Click slice on Top-Left and slice sprite to many small items.

 

 

sprite

We will use these 5 frames to create animation for Mario. Click on frames and rename them. (No1: Mario_Stand, No2: Mario_Walk_1, No3: Mario_Walk_2, No4:Mario_Walk_3, No5:Mario_Jump ) Apply changes and back to scene. Expand sprite file and you will see your 5 renamed sprites. Select 3 walk sprites and drag to scene. Give a name of your walk animation. Press play and test Mario’s movement. Now open animation window and create new clip. Drag Mario_Stand and change Samples value with one because we have only one frame. Do it for Mario_Jump too. Now open animator window.

Screen Shot 2015-03-16 at 14.27.52

 You see animations names not sprites here. Click MarioStand(or whatever you gave name for your stand animation) and Set as Default. You can create transitions between animations by right clicking on them. Right click on stand animation, click on Make Transition and click on walk animation. These two animations has a connection between them. Connect these four situations like in picture above. This map says “At start, MarioStand animation will be played. You can call MarioWalk animation while current animation is MarioStand. At any state, you can call MarioJump animation”. We need two parameters to call these animations automatically. Add Speed parameter as float and add isTouched parameter as bool.

Screen Shot 2015-03-16 at 13.52.56Select the arrow from stand to walk and you will see conditions in Inspector window. Select Speed and edit this parameter that if it is greater than 0. That means if speed parameter is greater than 0, then call walk animation. Select the arrow from walk to stand and edit it “Speed less than 0.0001”. That means if speed parameter is greater than 0, then call stand animation. Mario can jump at any situation so, select the arrow from Any State to jump and edit isTouched = false. Lastly, Mario can walk or stand after every jump action. Select the arrow from jump to stand and set isTouched = true. isTouched boolean will check if Mario collides with ground or not in script.

Add Rigidbody2D and BoxCollider2D to your Mario objet. You can reate a ground with sprite textures. Add Ground layer to ground. Now create a C# script for Mario.

Add an empty child named GCheck to Mario. Give it an Screen Shot 2015-03-16 at 14.12.52icon to see it clearly. Replace it right bottom edge of Mario object because it checks if Mario collides with ground when it overlaps with ground. It must touch ground! Now test it with right-left arrows and space key. It should works but if doesn’t, check layers, child position and code. If it works, Mario should moves around, he can jump and the right animation should be played at the right time. You can optimise the movement by changing speed values.

I gave an example for basic animation and movement. Please feel free to reply this post if it is complicated or just for asking more actions such as roaming enemies, power-up mushrooms, monster plants, collecting coins etc. Also I can share all source code if you want.

©Coffee Break Codes – 2D Mario Game Tutorial – Unity3D (C#)


Leave a comment

Your email address will not be published. Required fields are marked *

33 thoughts on “2D Mario Clone Game Tutorial – Unity3D (C#)

  • Jin

    Oh my goodness! an incredible article dude. Thank you On the other hand I’m experiencing concern with ur rss . Don’t know why Unable to subscribe to it. Is there anybody obtaining identical rss issue? Any one who knows kindly respond.

  • Cameron

    I’m not a c# coder, but I really like your tutorial here. I got all the way up to inserting the script and had errors.
    Does this tutorial work in unity 5? I was having script problems even after changing < and >

    • Mocha Post author

      Actually I did’t try it in Unity 5 but there is a automatic script updating feature in the latest version of Unity. Import your code and it converts automatically.

    • Mocha Post author

      I can help you if you specify your question:) On animator window, you just assign the order of animations. In additional, for this tutorial, you can create parameters to call an animation. For example, I created a Speed parameter and if speed > 0, I called Run animation.

  • Waffles

    I tried using your code here, what is gt and lt? It can’t find such variables. Also, that syntax mSpeed & gt; 0? it doesn’t recognize such syntax.

    Thanks

      • Waffles

        Thank you, but there are now 2 other problems, in the line:
        this.rigidbody2D.velocity = new Vector2(mSpeed * speed, this.rigidbody2D.velocity.y);
        It’s telling me that property rigidbody2D is deprecated, and component does not contain a definiton for a “velocity”.

        the line marioAnimator = GetComponent();, does this mean I need the mario object to be called marioAnimator? It’s giving me the error that “The Type Arguments for method ‘Component.getComponent()’ Cannot be inferred from usage, try specifying the the type arguments explicitly”.

        Finally, are you willing to share your final project for this game? I would like to use it for learning purposes only as I am new to unity3d.

        Thanks

        • Mocha Post author

          1-Mario object is the animated mario. You should add components to it before coding.
          2-You don’t need to create a marioAnimator named object in scene. Just create your animated mario and attach this code. If you create the animation correctly, it should work.
          3-I’m sorry, I don’t keep the project files after publish.

    • KaganKartal Post author

      Thanks for your comment Caffey. You can create a complete level with these 2 Mario tutorials. You just need to duplicate the actions and imlement a long scene. What do you want to learn about this game tutorial?

  • CHAN YIN

    Hi, Just to check how soon can tutorial start?
    I need to learn Game Design 2D Unity &C# , something like Mario Bro game.

    Hope to hear from you soon.

  • Bridgette Oreilly

    Hi coffeebreakcodes.com

    Dominate Google Page 1 In Minutes
    Newbie Friendly Software: Ranks on Google & YouTube Within 60 Minutes Regardless of Niche – With Just 4 Clicks… Guaranteed
    Breakthrough Software Gets You Unlimited FREE TRAFFIC From Google & YouTube… In Minutes

    Ranking almost instantly on Google is like finding ‘The Holy Grail when it comes to driving free online traffic.
    How much Free Targeted Traffic can you drive in the next few hours?

    * Imagine: What you can achieve now you can jump straight to the top of Google…
    * Imagine: How much traffic you could drive to your websites, ecom stores, to your offers, or direct to affiliate offers
    * Imagine: How much you can make each day when you can Rank on Google in Minutes.

    IF YOU’RE INTERESTED, CONTACT ME ==> videosumo@mail.com

    Regards, Bridgette
    Austria, BURGENLAND, Eggen Ii, 9556, Druckereistrasse 73