Walking Simulator (part 2) – Interaction and Scripts

Continuing on from the previous post on my walking simulator, I had to spend hours of research on how to expand the scripts in my game. I wanted to include some new features beyond what we’ve learned class, so I began with writing out what it was I wanted to change and how I was going to go about doing it:

  1. I wanted to update my doors, since they’re an integral part of my house. Rather than have the key automatically open the door once it’s picked up, I wanted the player to interact with the door to open it. Of course, I also wanted it to close should the player click on it again. Sound effects should also be included; at least an opening audio track and a closing audio track.
  2. To make the house more realistic and atmospheric, I needed lights. Clickable lamps, a light switch that hooks up several lights, a fuse box that could go out in the storm and would have to be activated before the electricity would work again, and finally some flicking lights for a horror or uneasy feeling.
  3. There were some issues with my highlighter script to begin with, thanks to some edits I’d done to my canvas while I was toying with zoomable objects, so I thought it was a good time to change the kind of highlighter I was using. Instead of just having text show up, I wanted to change the texture of the item the player was hovering over, making it stand out from the scene more.
  4. I was positive I needed triggers and to sort out how to put in jumpscares. It was essential to the game and would add suspense throughout. I thought it would also be more interactive to have the onscreen character comment about the trigger (whatever it may be), which meant I needed to learn how to delay actions by the length of the triggers.
  5. The radio is a source of ever-increasing frustration, where it will work sometimes and other times it doesn’t. Since I was unsure whether it would work during the peer review, I wanted to have other sound going on in the scene (perhaps the sounds of the storm outside).

I began with building on the things we learned in class about doors and keys, then added some extra functions.

public string keyItemName;
public GameObject door;

public AudioClip unlock;
public AudioSource mainAudio;

void OnRayDown ()
// Find the InventoryController
InventoryKL inventory = GameObject.FindObjectOfType<InventoryKL> ();
//Does the inventory list contain the key item?
if (inventory.items.Contains (keyItemName) == true) {
//Indicate on the OpenDoor script that the player picked up the key
door.GetComponent<OpenDoor> ().haveKey = true;
//Play the designated unlock sound
mainAudio.PlayOneShot (unlock, 0.2f);
//Initial rotation of the door to open it
iTween.RotateBy (door, new Vector3 (0, 0.20f, 0), 10);
//Destroy this script
Destroy (this);

I wanted to include the sound of the key unlocking the door, then continued with (if the player has picked up the correct key) changing the bool in a new script I made for opening/closing the doors. This part is the haveKey = true. The rest of the notes within the script explain the other parts to the best of my ability; I find annotating my scripts extremely useful, especially if I don’t touch the script for a while and have forgotten why I’ve put something in.

This DoorController script linked up to the OpenDoor script:

public GameObject door;
public bool open;
public bool haveKey;
public bool closed;

public AudioSource mainAudio;
public float hingeVolume;

public AudioClip openSound;
public AudioClip closeSound;

void OnRayDown ()
if (haveKey && closed) {
//Rotate the door to open position
iTween.RotateBy (door, new Vector3 (0, 0.2f, 0), 5);
//Indicate that the door is open (not closed) in the game
door.GetComponent<OpenDoor> ().open = true;
door.GetComponent<OpenDoor> ().closed = false;
//Play the designated open door audio
mainAudio.PlayOneShot (openSound, hingeVolume);
} else if (haveKey && open) {
//Rotate the door to closed position
iTween.RotateBy (door, new Vector3 (0, -0.2f, 0), 5);
//Indicate that the door is closed (not open) in the game
door.GetComponent<OpenDoor> ().open = false;
door.GetComponent<OpenDoor> ().closed = true;
//Play the designated closed door audio
mainAudio.PlayOneShot (closeSound, hingeVolume);

This is a quick script I threw together to test after understanding the absolute usefulness of bools in scripts – they are now invaluable to me, even at this stage of the project. With them, I could indicate if various requirements have been met before parts of the script would be executed.

Here, I was using the script to tell the game whether the player had picked up the key (which meant that the door could be opened and closed without needing any more keys) and if the door was already in a open or closed position, so it would do the opposite action if the player clicked on it again.

This script is far from perfect, since the player can interrupt the iTween part way through running and ruin the motion of the door swinging open/closed. I’ll need to look into this when I have more time; I want to see if there’s a way to prevent the player from clicking on an object for a set amount of time. There also might be other ways of achieving the same results in a neater fashion, without using bools, but I prefer it.

With these two scripts and the inventory script we already initialised in class, I was able to hook up all my doors to any of the keys I wanted to include in the game, but it also future-proofed my game for other plans I have: namely, I want to include an alarm system for the outside door, where a code has to be put into a keypad before any of the outside doors can be unlocked and opened.

I next turned my focus to the lights in my game – first the ones in the main entrance while I perfected the script and then I would be able to model/import more to put throughout the house. I listed the three objectives I wanted to achieve in my list at the beginning of this blog, but firstly I had to figure out how to make a light turn on and off at the player’s discretion.

With the help of online resources, I took the base of a lamp script and adapted it to my needs:

public bool isOn;
public bool FlickLight;
public bool switchOn;

public AudioClip LampSwitchOn;
public AudioClip LampSwitchOff;
public float volumeSwitch;

public MeshRenderer LampMesh;
public Material ActivedLampMaterial;
public Material DisabledLampMaterial;

public Light lampLight;
public AudioSource audioSource_switch;
//AudioSource for the Switch Sounds
public AudioSource audioSource_noise;
//AudioSource for the Noise Sound
void Update ()
if (isOn) {
LampMesh.material = ActivedLampMaterial;
} else {
LampMesh.material = DisabledLampMaterial;

void OnRayDown ()
if (!isOn) {
audioSource_switch.PlayOneShot (LampSwitchOn, volumeSwitch);
lampLight.enabled = true;
isOn = true;
if (FlickLight) {
GetComponentInChildren<LightFlickerPulse> ().enabled = true;
audioSource_noise.enabled = true;
} else if (isOn) {
audioSource_switch.PlayOneShot (LampSwitchOff, volumeSwitch);
lampLight.enabled = false;
isOn = false;
audioSource_noise.enabled = false;
GetComponentInChildren<LightFlickerPulse> ().enabled = false;

This script is so interesting to me, since I now understand it far more than I ever thought I would when I started out designing this game. There were a number of things I needed to have included: audio/sound for the light source turning on/off and a float for the sound, which I now realise is an excellent idea for any audio you might want to include in a game with so many different sounds.

I then knew that I wanted the lights to change looks depending on whether the were on or not. The bulbous glass lights I have in the main entrance should be opaque or even see-through when they aren’t on, but then glow with a strong off-yellow colour when they are on. Any other lamps should do the same: dull when they’re off and bright when they’re on, just like in real life.

The flicker script I use is far too complicated for me to wrap my head around. I found it in an asset package on Unity and decided it was what I wanted to implement: in the most basic of explanations, it constantly updates the intensity of a light to various levels every frame the game plays, in order to give the appearance that the light is flicker.

To get the lights hooked up to a switch, I merely created a duplicate script and added in an extra requirement via a bool of “switchOn”. This meant that the lights wouldn’t turn on at all unless the switch had been pressed by the player. Of course, I also scripted something simple using the same layout as the doors for the wall light switch. When the player pressed the switch, the switchOn bool in the lights script would change to true and thus the script could run until the player turned the lights off.


My trigger script was next on my agenda, since it was something new to learn about that we’d not yet touched on in class. I’d seen people use jumpscares in tutorials, so I thought they’d be good practice. This was especially true for the kind of game I had in mind. I started by creating a collider from a basic 3D object (in my case, a cube resized/re-positioned at the bottom of the stairs.

I then edited the box collider to make the object a trigger and taking away the visible material (so it wouldn’t be seen in the game).

Finally, I wanted audio and narrative to accompany the jumpscare, so within the trigger script I added an option to play a sound, which was going to be a gunshot. I then wrote a narrative script that would play after the trigger finished.

Overall, I’m pleased with the progress I’ve made in developing my scripts and what I’ve learned so far, but there is still so much more that I want to do to the game. I really want to make it an immersive game of suspense and mystery, so I’ll continue looking into other sources of inspiration (tutorials, other games by my classmates, play-throughs online, etc.) that will help me achieve that feeling.


Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s