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

Coffee Break Codes
4 min readNov 13, 2018

--

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 to meet princess. In this tutorial, I will show you how to create animations and little examples for some functions of game.

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

Let’s 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 into 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.

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 optimize the movement by changing speed values.

I gave an example for basic animation and movement. You can add more actions such as roaming enemies, power-up mushrooms, monster plants, collecting coins etc.

See full tutorial on Coffee Break Codes.

Need more? Visit Part-2 of this tutorial!

--

--