Lesson 3: Open sesame! Create a magic door

Checked with version: 2018.2.1 - Difficulty: Beginner

In Lesson 3, you'll learn how to add a door and trigger it to open (disappear) by stepping on a pressure pad.

Set yourself up for success by setting aside at least 1 hour to complete this lesson. If you get stuck, seek out answers and help in our moderated 2D Game Kit forum.

Adding in a door

In the 2D Game Kit, Events allow you to create actions. You’ll use Events to trigger a door opening (disappearing) when the player steps on a pressure pad.

Let’s start by finding a good place to add a Door and make sure Ellen is to the left of it to easily mimic gameplay when we test.

  • If you already have a good location to add a Door, then make sure that Ellen is to the left of where you want to add the Door. To move Ellen, make sure you're in the Scene view and not in Play mode. Select Ellen in the Hierarchy, then press Ctrl (Windows)/Cmd (Mac) and click on Cameras. Now you've got both Ellen and Cameras selected in the Hierarchy. Now click and drag on the transform tool (the blue square) or the move tool (the red arrow) to place Ellen in the desired location
  • If you don't have a good location in your scene to add a Door, then build one using the Tilemap feature like you did in Lesson 1. (Window > 2D > Tile Palette)

Now let’s add in a Door:

  • Make sure you're not in Play mode
  • In the Project Window, go to 2DGameKit > Prefabs > Interactables
  • Find the Door Prefab and drag it into the Scene View. Place it somewhere to the right of Ellen so it blocks her path

Triggering the door with a pressure pad

Now let’s add the pressure pad into the Scene:

  • In the Project window, go to Prefabs > Interactables and find the PressurePad Prefab
  • Drag it onto the ground in between Ellen and the Door

Note: If the player seems to be blocked by the pressure pad instead of stepping on it, lower it slightly. The player can easily get stuck against the collider. However, if it's placed too low, then its events won't trigger either.

Some events are already defined; for example, when stepped on, the PressurePad plays a sound and lights up. Press Play and use the movement keys (A and D) to run on the the pad to test it out. It should make a sound and light up. If it doesn't, it's probably too low and you should raise it up a bit. Once it's in the right spot, connect the PressurePad to the Door.

  • In the Hierarchy window, select the PressurePad
  • In the Inspector, find the Pressure Pad component
  • In the On Pressed list, click the + at the bottom right to add a new event
  • Drag the Door GameObject from the Hierarchy window to the None(Object) field in the event
  • In the No Function dropdown, find Animator > Play (string)

  • In the text box that appeared under the dropdown, enter the text DoorOpening  (It's case-sensitive)

  • Press Play and use the movement keys (A and D) to run onto the Pressure Pad. The door should open! (Aka disappear.)

You might be wondering: "How would I know to type DoorOpening?" Answer: All the Animation Clips are stored in the Project window, in Art > Animations > Animation Clips. Since we know the Door is an Interactable in the Prefabs folder, go to the Interactables folder in the AnimationClips dropdown to find the animations available to Interactables. To play different Animators in an Event, you must match the name (string) of the Animation Clip exactly (case-sensitive). If the string is cut off, click on it, and in the Inspector you can see its full string.

If you wanted to shoot a switch instead of placing a pressure pad, you can do that by using the same steps, but selecting the ResusableSwitch in the Prefabs folder instead. Give it a try!

Remember to save your Scene! In the menu select File > Save Scene or press Ctrl + S (Windows) or Cmd + S (Mac)

When you complete Lesson 3, check it off below!

Supporting live training video

Lesson 3: Add a door and trigger it by stepping on a pressure pad

Note: In the video below, the instructions for Lesson 3 automatically begin at 2:19, which is what is covered in the text material above. If you also want to learn how to add destructible objects, you can watch and follow along with the video from the beginning of it, but that's just extra credit!

Did you finish Lesson 3?

Go to homepage Continue to Lesson 4