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

in #unity8 years ago

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.

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

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.

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.

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

codes.JPG

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.

http://coffeebreakcodes.com/sample-projects-unity/2d-mario-game-unity3d/

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by AirdropSinyal from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

awesome clone! mario is such a classic today's platformers are still ripping off it

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.081
BTC 60930.75
ETH 1577.59
USDT 1.00
SBD 0.47