6. Doors & Keys

Updated: 2018-08-31

In this part, we'll implement the new challenge in level 2: opening a door with a key. Start by opening the level 2 scene, which already contains the setup and prefabs.

1. Add a HasKey variable

When the player reaches a door, we'll need to check if they have a key. For this purpose, we'll use a boolean (true / false) variable on the player.

  1. Select the Player object
  2. Add a new boolean object variable called HasKey, default to false
  3. Apply the changes to the prefab so other players share this variable

2. Create the key flow machine

When the player collides with the key in the scene, the HasKey variable should become true, and the key should disappear.

  1. Select the Key object
  2. Add a new flow machine
  3. Create a new macro for it called Key
  4. Apply the changes to the prefab

The graph is really simple now that we can reuse our On Collision With macro:

Destroy is located under Codebase > Unity Engine > Game Object. It's important to note that unlike most functions, this one does not automatically take Self as its target, which is why we must explicitly connect a Self unit.

3. Create the door flow machine

Here we go again:

  1. Select the Door object
  2. Add a new flow machine
  3. Create a new macro for it called Door
  4. Apply the changes to the prefab

In the graph, we'll check the HasKey variable on the player. If the key is found, we disable the box collider on the door and change its sprite to look open. If the key is missing, we just output a message to the console for debug.

  • Set Enabled is located under Codebase > Unity Engine > Box Collider 2D
  • Set Sprite is located under Codebase > Unity Engine > Sprite Renderer
  • Log is located under Codebase > Unity Engine > Debug

If you test your game now, the door should open only if you've grabbed the key:

4. Display the key in the HUD

The last thing we have to do is to handle the HUD display for the key.

When the player grabs the key, the empty key icon should become filled.

If there is no key in the scene, the key icon should be hidden altogether.

  1. Open the HUD scene
  2. Select the Key object under HUD > Row
  3. Add a new flow machine
  4. Switch its source to Embed

We switch the machine's source to Embed because we will only ever use this graph once, in the HUD scene. In other words, there is no need to create a reusable macro because the HUD is not a prefab. You can learn more about machine sources and prefab restrictions in the manual here.

To hide the key icon if no key exists in the scene, we can set the game object to inactive if there are no game objects tagged with Key:

  • Find With Tag and Set Active are located under Codebase > Unity Engine > Game Object
  • Null Check is located under Nulls

To change the sprite depending on whether or not the player has the key, we need to check the HasKey variable on the Playerobject:

You can find the sprites HudKeyFull and HudKeyEmpty under Sprites / HUD.

There is a small performance problem with our graph: Find With Tag is known to be a bit slow. It's not an issue if we only use it once on Start, but since we're calling it every frame in Update, it would be ideal to use a faster way to find the player. This is slightly premature optimization (our game would run perfectly fine with Find With Tag), but we'll show it for the sake of teaching "best practices".

We'll use a application variable called Player and holds a reference to the player game object. It will get automatically created by the player graph, so we won't need to set it up in every level manually. This is a simple implementation of the "singleton" pattern: because we know there's always only 1 player in the game, it's safe to assign its instance to an application variable.

  1. Save the HUD scene
  2. Open the PlayerController flow macro
  3. Add this group of units somewhere in your graph:

Now that this is done, we can go back to our HUD key graph and replace Find With Tag by Get Application Variable:

There is a small error in the above screenshot. The variable name should be HasKey, not Key!

That's it! Now, if you test your game, you should see the key sprite change in the HUD:

Was this article helpful?
Be the first to vote!
Yes, helpful
No, not for me