Room Game – Unity Pt.2

We began this week with a simple method for understanding code workflow visually before typing any script, activity diagrams. Here is an example that would be an activity diagram for our key script from last week.


As you can see an AD simply talks through the steps required of your code in plain English, making sure that the flow of the diagram has the sequence in the correct order of events. As someone with a primarily art background I’m still struggling with code so hopefully a visual representation will assist with learning.


This weeks new feature was a flashlight! Actually thrilled with this as I wanted to eventually theme my walking simulator in a dark industrial/sci-fi environment and this works to my advantage.

A spotlight was dropped into the scene and added as a child of the first person controller. Wherever you go, the flashlight goes, makes sense. Embarassingly it took me a while to realise it was even working due to the fact I’d never capped my room off with a roof yet so there was still plenty of directional light pouring into the scene from Unity’s default light.

Heres the script.

using UnityEngine;
using System.Collections;

public class Flashlight : MonoBehaviour {

public Light flashlight;

// Update is called once per frame
 void Update () 
 if (Input.GetKeyDown (KeyCode.F)) {
 // Flip the intensity between 0 and 1
 flashlight.intensity = 1 - flashlight.intensity;


To break it down quickly (otherwise I’m going to spend every week writing my own programming book) a new public data type called light is created with the associated variable flashlight. If the player pushes the F key the flashlight toggles on/off.

If the light intensity of flashlight is equal to 1 then it minuses itself to zero and turns off. Then loop back, flashlight intensity is reset back to 1 on key press, minus current intensity of zero and the light comes back on as its back to intensity 1.


Our winning feature creep suggestion from last week was an inventory system and this was expanded upon today. The concept was kept simple and there was no GUI element to represent an inventory or items within it, just a text string in the background keeping tags on the things we have.

We created three scipts:

  • Inventory
  • InventoryItem
  • DoorController

The first of these ‘Inventory’ is what I was just discussing and looks like this.

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

public class Inventory : MonoBehaviour {
 //Make a list to store collected items.
 public List<string> items = new List<string>();


A public list function is created and defined to contain a string (will store letters/numbers but can’t be used to calculate numbers, similar to text fields in Excel/SQL) and we name the variable items. If a new item is collected it’ll throw it onto the list!

The next is ‘InventoryItem’

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class InventoryItem : MonoBehaviour {

public string itemName;

void OnMouseDown() {
 //Find the Inventory controller
 Inventory inventory = GameObject.FindObjectOfType<Inventory> ();
 //Add this item to the inventory
 inventory.items.Add (itemName);
 //Destory (pick up) this item
 Destroy (gameObject);
 GameObject.FindObjectOfType<Text> ().text = "";


This will be attached to all items we decide are collectable. It breaks down like this, when the mouse is clicked, looked for the inventory string we just setup, add our collected item to this string (using the name defined in the component) and then destroy this object in the game world so it no longer appears.

Finally we have ‘DoorController’

using UnityEngine;
using System.Collections;

public class DoorController : MonoBehaviour {

//The name of the object that controlsthis door
 public string keyItemName;

void OnMouseDown () {
 //Find the InventoryController
 Inventory inventory = GameObject.FindObjectOfType<Inventory> ();
 //Does the inventory list contain the key item?
 if (inventory.items.Contains (keyItemName) == true) {
 //rotate this door
 transform.Rotate(0, 80, 0);
 //destroy this script
 Destroy (this);

This will be attached to the door and when clicked on the script will look through the ‘Inventory’ list and if it contains the name of our key the door will rotate on its pivot and open. The script will then be destroyed so it can’t run again. Originally this was scripted to automatically open the door once it found the key in the inventory but with a little help from a fellow student we managed to add some extra interaction, requiring the door to be clicked on.

The key controller from last week was modified again adding a distance requirement before the object could be collected.

using UnityEngine;
using System.Collections;

public class KeyController : MonoBehaviour {

public GameObject door;

void OnMouseDown() {
 // Store the distance from this key to the main camera
 float distance = Vector3.Distance (transform.position, Camera.main.transform.position);

//If the distance is less than 3 units...
 if (distance < 3) {
 //Pick up the key

The concept is simple even if I don’t fully understand the code breakdown. Keep a running track on the distance between the object and player (in our case camera). If this distance is less than three units then pick up the key. This would actually be useful on a lot of things to prevent interactions from halfway across a map/level.

Next weeks feature

For next weeks feature everyone made suggestions in the form of an activity diagram and uploaded them to the shared drive, we’ll find out next week what makes the pick. I suggested a way to pick up letters/logs and to display an art asset of that letter on screen so the player can clearly read it. I’ve seen a lot of games/walking sims use this kind of feature to push a narrative without requiring character interactions.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s