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

mario

We will make a standout amongst the most celebrated amusement character in this instructional exercise: MARIO! I figure everyone knows this handyman fellow’s story. He makes due against the beasts for the meet princess. In this instructional exercise, I will demonstrate to you generally accepted methods to make activitys and little cases for some funtions of amusement.

Before the begining, please download the sprite sheet here or get your own from google. We require this sprite sheet to make livelinesss and a few items.

Gives begin with Mario a chance to character. Make a Sprites envelope and import sprite sheet here. Select sprite and make the Sprite Mode Multiple. On the off chance that you get another sprite, Pixels Per Unit esteem might be extraordinary yet it’s 32 for my sprite. Open sprite editorial manager.

 

We will utilize these 5 casings to make liveliness for Mario. Tap on outlines 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. Extend sprite document and you will see your 5 renamed sprites. Select 3 walk sprites and drag to scene. Give a name of your walk movement. Press play and test Mario’s development. Presently open activity window and make new clasp. Drag Mario_Stand and change Samples an incentive with one since we have just a single casing. Do it for Mario_Jump as well. Presently open artist window.

sprite

    sprite
mariostand
mariostand

You see livelinesss names not sprites here. Snap MarioStand(or whatever you gave name for your stand activity) and Set as Default. You can make changes between livelinesss by right tapping on them. Right tap on stand activity, tap on Make Transition and tap on walk movement. These two livelinesss has an association between them. Interface these four circumstances like in picture above. This guide says “At begin, MarioStand activity will be played. You can call MarioWalk activity while current liveliness is MarioStand. At any state, you can call MarioJump liveliness”. We require two parameters to call these livelinesss consequently. Include Speed parameter as buoy and include isTouched parameter as bool.

speed
speed

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;

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);
}
Include a vacant youngster named GCheck to Mario. Supplant it right base edge of Mario question since it checks if Mario crashes into ground when it covers with ground. It must touch ground! Presently test it with right-left bolts and space key. It should works yet in the event that doesn’t, check layers, youngster position and code. On the off chance that it works, Mario should moves around, he can bounce and the correct liveliness ought to be played at the opportune time. You can advance the development by changing rate esteems

We made a few livelinesss for Mario character, for example, walk, stand and hop. Lets make the world and a few crowds. You can get sprite sheets from Part 1 or you can get your own particular sprites from google and so forth.

At to start with, we will make 4 activitys (question mark, coin, plan and crowd). You know how to do it.

For plant character, we require an auto-move liveliness for vertical development. Initially do the fundamental liveliness with sprites to open-close its mouth. At that point, open scene and movements windows, put plant at the base of pipe, press REC catch on activitys window. Presently you can record the development liveliness. Snap second 1.0 and drag plant upward, at that point click second 2.0 and drag plant descending. Snap REC catch once more. It’s finished.

Presently, lets move the mob. Make a C# content named Mobs and alter it.

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);
}
}

Include Box Collider 2D and Rigidbody 2D to swarm. Snap “Settled Angle” in Rigidbody 2D. As you see, this content uses names of items in crash work. Along these lines, change content with then names you utilize.

On the off chance that you make a question mark box, be watchful when you include Box Collider 2D.

Make a void diversion protest and include blocks and box in it. Include a Box Collider 2D for parent question. Collider of question mark box ought to be a smidgen down. We would prefer not to summon mushroom while strolling on it. Make a C# content named Mushroom and lets summon our mushroom.

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 content moves and wrecks the mushroom.

Presently, we ought to alter Mario content. Mario ought to get greater when he gathers a mushroom, he should bite the dust when he hits a swarm or plant. Likewise we ought to summon a mushroom when Mario hits the question mark.

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;
}
}

Alter your Mario content with this. Also, allot objects.

Finally, we require a camera content for a dynamic camera development.

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);
}
}

There’s nothing more to it. In extra, you can include numerous swarms, plants, mushrooms and so forth and furthermore you can include some world things for representation.

Click for Download Source Project

Leave A Reply

Your email address will not be published.