2D Mario Game Tutorial – Unity3D (C#)
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. Create 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
public float speed = 1.0f; public float jumpSpeed = 0.5f; public LayerMask groundLayer; private Animator marioAnimator; private Transform gCheck; private float scaleX = 1.0f; private float scaleY = 1.0f; void Start () { marioAnimator = GetComponent(); gCheck = transform.FindChild("GCheck"); } void FixedUpdate () { float mSpeed = Input.GetAxis("Horizontal"); marioAnimator.SetFloat("Speed", Mathf.Abs(mSpeed)); bool isTouched = Physics2D.OverlapPoint(gCheck.position, groundLayer); if (Input.GetKey(KeyCode.Space)){ if (isTouched){ rigidbody2D.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Force); isTouched = false; } } marioAnimator.SetBool("isTouched", isTouched); if (mSpeed > 0){ transform.localScale = new Vector2(scaleX, scaleY); } else if (mSpeed < 0){ transform.localScale = new Vector2(-scaleX, scaleY); } this.rigidbody2D.velocity = new Vector2(mSpeed * speed, this.rigidbody2D.velocity.y); } |
Add an empty child named GCheck to Mario. Give it an icon 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#)
Awesome and Epic! Thank You! ^^
Definately I want to see more.
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.
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 >
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.
may you teach better the animator part? the animations don’t run well here 🙁
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.
Hi, Its a marvellous effort. Can you please provide me with further source code?
Part 2 is added.
I love your tutorial. i’m huge fan of Mario.Could you share all source code with me? Thank You.
Part 2 is added.
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
Sorry for it.. They mean greater than, less than etc.
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
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.
Hello. Would you mind share me the project. Thanks for you help.
Sorry, I don’t keep project files after publishing.
Ooops, page not found in Demo!
Play Demo
Need all codes
Can you please share the code with me as well 🙂 .. Thank you
Sorry but I don’t keep the code.
Hi , i like this tutorial, i need source code of Mario.
can you please share its dropbox link with us.
I updated the code so that it works with 5.4 for all you lazy people http://pastebin.com/jtNJ2FQb
If you cannot make it work then god help you 🙂
fantastic,I like it, may you teach me more about this mario tutorial?please,thanks
fantastic, I like it very much,may you teach me more about this mario tutorial?please,thanks
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?
Hello. Would you mind share me the project. Thanks for you help.
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.
Just wondering if these sprites are public domain. Is it an infringement if I include them in a game on my website?
Good.Can you send me whole source code?
awesome
Is this site still alive? I have a problem with the speed and isTouched part. In my unity there is nothing like that. Can you help me?
source code plz ??
ismailbarka82@gmail.com