Mario Key Door in UnityHow to Recreate the Key Door from Super Mario Maker 2

Author Waldo
Published August 15, 2019

In this video I show you how to recreate the key follow and key door effects from the game Super Mario Maker 2

Video Walkthrough

  • 0:25 - Current Scene Setup
  • 1:00 - Setting up the Key Object
  • 1:30 - Installing a Spring Joint 2D component
  • 3:35 - Creating a script for our Key Follow Object
  • 5:00 - Setting up a backpack for our Character
  • 5:30 - Connecting our Spring Joint 2D to our player via C# script
  • 6:30 - Setting up our Doors in the scene
  • 7:00 - Creating a script for our Door Teleportation
  • 9:00 - Adjusting Door script to wait for release of "Up" Key
  • 10:10 - Final Product

Source Code for Key.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Key : MonoBehaviour{
    private SpringJoint2D spring;

    // Start is called before the first frame update
    void Start(){
        spring = GetComponent<SpringJoint2D>();
        GameObject backpack = GameObject.FindWithTag("Backpack");
        spring.connectedBody = backpack.GetComponent<Rigidbody2D>();
        spring.enabled = false;
    }
    private void OnTriggerEnter2D(Collider2D collision) {
        if(collision.gameObject.tag == "Player" && !spring.enabled){
            spring.enabled = true;
        }
    }

}

Source Code for Door.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Door : MonoBehaviour{
    public GameObject connectedDoor;
    public bool teleported = false;
    private GameObject player;

    // Start is called before the first frame update
    void Start(){
        player = GameObject.FindWithTag("Player");
    }

    // Update is called once per frame
    void Update(){
        if(teleported && Input.GetAxisRaw("Vertical") < 1){
            teleported = false;
        }
    }
    private void OnTriggerStay2D(Collider2D collision) {
        if(collision.gameObject.tag == "Player" && Input.GetAxisRaw("Vertical") == 1) {
            if(Input.GetAxisRaw("Vertical") == 1 && !teleported){
                player.transform.position = connectedDoor.transform.position;
                connectedDoor.GetComponent<Door>().teleported = true;
            }
        }
    }
}

This tutorial is sponsored by this community

In order to stick to our mission of keeping education free, our videos and the content of this website rely on the support of this community. If you have found value in anything we provide, and if you are able to, please consider contributing to our Patreon. If you can’t afford to financially support us, please be sure to like, comment and share our content — it is equally as important.

Join The Community

Discussion

Browse Tutorials About