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. Convert its source to a macro 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. Convert its source to a macro 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, leave it at embed

We can leave the machine at 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.

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 Player object:

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 scene 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 scene, it's safe to assign its instance to a scene 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 Scene Variable:

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

Head-Up Display (HUD) Health & Damage

This article was helpful for 1 person. Is this article helpful for you?