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#)