2D Mario Clone Game Tutorial (Part 2)
Thank you for your interest in 2D Mario Clone Game Tutorial. We will continue to develop Mario.
We created some animations for Mario character such as walk, stand and jump. Lets create the world and some mobs. You can get sprite sheets from Part 1 or you can get your own sprites from google etc.
At first, we are going to create 4 animations (question mark, coin, plan and mob). You know how to do it.
For plant character, we need a auto-move animation for vertical movement. First do the basic animation with sprites to open-close its mouth. Then, open scene and animations windows, place plant at the bottom of pipe, press REC button on animations window. Now you can record the movement animation. Click second 1.0 and drag plant upward, then click second 2.0 and drag plant downward. Click REC button again. It’s done.
Now, lets move the mob. Create a C# script named Mobs and edit it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
private Vector2 currentVelocity; void Start () { rigidbody2D.velocity = new Vector2(1,0); } void Update () { currentVelocity = rigidbody2D.velocity; } void OnCollisionEnter2D(Collision2D c){ if (c.collider.name == "Obstacle" || c.collider.name == "Pipe" || c.collider.name == "Mario"){ rigidbody2D.velocity = Vector2.zero; rigidbody2D.velocity = new Vector2(-1 * currentVelocity.x, 0); } } |
Add Box Collider 2D and Rigidbody 2D to mob. Click “Fixed Angle” in Rigidbody 2D. As you see, this script uses names of objects in collision function. So, change script with then names you use.
If you create a question mark box, be careful when you add Box Collider 2D.
Create an empty game object and add bricks and box in it. Add a Box Collider 2D for parent object. Collider of question mark box should be a little bit down. We don’t want to summon mushroom while walking on it. Create a C# script named Mushroom and lets summon our mushroom.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
private Vector2 currentVelocity; void Start () { rigidbody2D.velocity = new Vector2(1,5); } void Update () { currentVelocity = rigidbody2D.velocity; } void OnCollisionEnter2D(Collision2D c){ if (c.collider.name == "Obstacle"){ rigidbody2D.velocity = Vector2.zero; rigidbody2D.velocity = new Vector2(-1 * currentVelocity.x, 0); } if (c.collider.name == "Mario") { Destroy(this.gameObject); } } |
This script moves and destroys the mushroom.
Now, we should edit Mario script. Mario should get bigger when he collects a mushroom, he should die when he hits a mob or plant. Also we should summon a mushroom when Mario hits the question mark.
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
public class Mario : MonoBehaviour { public float speed = 1.0f; public float jumpSpeed = 0.5f; public LayerMask groundLayer; public Transform boxOff; public Transform mush; private Animator marioAnimator; private Transform gCheck; private float scaleX = 1.0f; private float scaleY = 1.0f; private bool isBig; 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); } void OnCollisionEnter2D(Collision2D c){ if (c.collider.name == "Box"){ Transform newBox = Instantiate(boxOff, c.transform.position, Quaternion.identity) as Transform; Transform newMush = Instantiate(mush, new Vector2(c.transform.position.x, c.transform.position.y + 1), Quaternion.identity) as Transform; Destroy(c.gameObject); } if (c.collider.name == "Mush(Clone)") { isBig = true; scaleX = 1.5f; scaleY = 1.5f; transform.localScale = new Vector2(scaleX, scaleY); } if (c.collider.name == "Plant" || c.collider.name == "Mob") { if(!isBig){ Application.LoadLevel(0); } else{ scaleX = 1.0f; scaleY = 1.0f; transform.localScale = new Vector2(scaleX, scaleY); StartCoroutine(Tiny()); } } if (c.collider.name == "Head") { Debug.Log("aaa"); Destroy(c.transform.root.transform.gameObject); } if (c.collider.name == "Coin") { Destroy(c.gameObject); } } public IEnumerator Tiny() { yield return new WaitForSeconds(1.0f); isBig = false; } } |
Edit your Mario script with this. And assign objects.
At last, we need a camera script for a dynamic camera movement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public float dampTime = 0.15f; private Vector3 velocity = Vector3.zero; public Transform target; void Update () { if (target) { Vector3 point = camera.WorldToViewportPoint(target.position); Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.3f, point.z)); Vector3 destination = transform.position + delta; transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime); } } |
That’s all. In additional, you can add multiple mobs, plants, mushrooms etc. and also you can add some world items for visualization.
You can download source code here.
I am looking forward to part 3 π
Sorry mate it is not fullversiongames.com π
Ok. thank you π
I can’t see brick in your sprites
It’s in Part 1..
sprites don’t have stone ground π
There are 2 square images in sprites..you can create ground by duplicating the left one..(at the first line, after 3 mario images)
how many mass of mario and mob, plz?
Pingback: 2D ΠΠ»ΠΎΠ½ Mario. Π§Π°ΡΡΡ 2 (Unity C#)
This is an amazing tutorial!!!