Walking Sim – Adding Scene Interactivity

Brand new doors

The big thing to consider in the scene was doors. Firstly, to show what we’ve learnt in class in a new project, but more than that doors can function as obstacles to help guide the player, puzzles to solve etc. Especially in a cramped environment full of corridors they are a big part of the scene. I wanted the doors opening and closing, also using the iTween to make the animation go up and down rather than on a hinge. Lastly to add sound to both opening and closing for some classic hydraulic swoosh noises.

To assist with guiding the player down a certain path I opted not to have all the doors open by default, to re-use the key idea from lessons but have certain keys for certain doors. I was going to take the idea further and create a keycard which would interact with a panel by the door rather than the door, this didn’t end up happening due to time but would be a nice addition later on.

Here is the code that ended up running my doors:

using UnityEngine;
using System.Collections;

public class DoorController : MonoBehaviour {

//The name of the object that controls this door
public string keyItemName;
public GameObject door;

void OnRayDown () {
//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
iTween.RotateBy (door, new Vector3 (0, 0.25f, 0), 5);
//destroy this script
Destroy (this);
}
}
}

The only thing I needed on top of this was a script that would let the player open and close the door whenever they clicked on it. As a combined effort with another student and some research, this is the script that does the job:

public class OpenDoor : MonoBehaviour
{

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

public AudioSource mainAudio;
public AudioClip openDoor;
public AudioClip closeDoor;

void Update ()
{
//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
door.GetComponent<OpenDoor> ().haveKey = true;
}
}

void OnRayDown ()
{
if (haveKey && closed) {
//Open door
iTween.MoveBy (door, new Vector3 (0, 3f, 0), 1);
mainAudio.PlayOneShot (openDoor, 0.2f);
door.GetComponent<OpenDoor> ().open = true;
door.GetComponent<OpenDoor> ().closed = false;
} else if (haveKey && open) {
//Close door
iTween.MoveBy (door, new Vector3 (0, -3f, 0), 1);
mainAudio.PlayOneShot (closeDoor, 0.2f);
door.GetComponent<OpenDoor> ().open = false;
door.GetComponent<OpenDoor> ().closed = true;
}
}

Using bools means I can tell the game what state the door is in, among other things. Does the player have the key item? Is the door is already in an open state (so it can only be closed and not open again) or is it closed and needs to be open?

Re-using the Radio

I wanted to keep the radio in from previous lessons as I liked the idea, I didn’t exactly want music and this is the point I realised after making the dark gloomy atmosphere I might as well try and add a bit of horror.

I dug around on the internet originally looking for some white noise and unprocessed EVP recordings (electronic voice phenomena). After this was a bust I started looking for general sound effects websites, eventually I’d need to start adding sounds into my games so it doesn’t hurt to find some free sources.

In the end I found:

I settled on a sound clip that consisted of some howling souls from hell, it would have been cool if this could have been mixed with some static white noise to make it sound like its coming from a radio but I’ll leave digging into an area I know nothing about for a future occasion. Demon sounds it is for my radio, changed the sound clip, moved the radio and battery to their new locations. Brilliant!

Lighting & linking all the lights

Partway into fleshing out the level I realised that all my lights were permanently on, while the intensity of all the lights is low it does make the flashlight functionality (still present from the class tutorial) a bit useless. I decided I’d like to start the game with all the lights off and the player is required to find their way to a switch/generator and flip the lights back on.

This script was a bit more complicated and I had to go searching for some help along the way, although I knew the general idea I had to use. In my mind, it was sort of like the door script with the key, except instead of the key, it was a generator that had to be active. There are probably better ways to accomplish what I have, but this is the way that made sense to me.

public bool generatorActive;
public AudioClip generatorOn;
public AudioSource audioSource_generator;
public float volumeSwitch;

public GameObject lampOne;
public GameObject lampTwo;
public GameObject lampThree;
public GameObject lampFour;
public GameObject lampFive;
public GameObject lampSix;
public GameObject lampSeven;
public GameObject lampEight;
public GameObject lampNine;
public GameObject lampTen;
public GameObject lampEleven;
public GameObject lampTwelve;
public GameObject lampThirteen;
public GameObject lampFourteen;
public GameObject lampFifteen;

void OnRayDown ()
{
//Light on
lampOne.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampTwo.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampThree.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampFour.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampFive.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampSix.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampSeven.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampEight.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampNine.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampTen.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampEleven.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampTwelve.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampThirteen.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampFourteen.GetComponent<LampWithSwitchKL> ().switchOn = true;
lampFifteen.GetComponent<LampWithSwitchKL> ().switchOn = true;
audioSource_generator.PlayOneShot (generatorOn, volumeSwitch);
}

I added a lot of bools to the script, one for each of the primary lights, and then I could drop the lamps in one by one so that their bool in their component would change to true and they would turn on. I’ve been told there might be a way to make the lights children of the generator and use a different code with less lines, so I’ll have to look into that and test it as some point.

Unfortunately, there were two lights in the lamp fixtures I modelled, but only one of them was actually part of the light source (on reflection combining the model would have been far easier than making more code, but I think I was so far down the rabbit hole it was getting solved with code or not at all). While the primary lights would change their shaders and the lights would turn on, the secondary lights stayed in a static state. They needed a simple script that would change their meshes dependent on the state of the primary lamp.

public bool enabled;

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

void Update ()
{
if (enabled) {
LampMesh.material = ActivedLampMaterial;
} else {
LampMesh.material = DisabledLampMaterial;
}
}

Finally, the primary lamps needed to be integrated with these other two scripts. This was more complicated to me than it sounds. Not only did they have to be dependent on the generator being on/off, but they also had to tell the secondary lights when they could change shaders.

capture

Once again, I relied heavily on bools to make sure all those necessary “boxes were ticked”. Then, using the same kind of script, I had their meshes change from being see-through to a neon glow. To add a more eerie feeling to the bottom floor of the ship, I went and found a script that made it seem like the lights were flickering. A typical horror movie trope, so I thought it was appropriate. Being honest I’ve not posted the code because I don’t understand most of it, I was lucky to get it to working having modified it from another source on the Unity store. At some point, I plan on trying my skills on a siren/emergency light that would activate after the player passes through a trigger.

Background music

After adding my little horror sound clip earlier I thought it would be nice to find a piece of looping background music that would always play while the game is running, to help atmosphere and add another level of solitude and oppression rather than just the darkness. On top of the links from earlier I also came across http://freemusicarchive.org/ which has a really large amount of free to use music. I settled on this track.

No complexity here, drop the music into the scene and select looping from the audio source menu.

SFX

I knew my doors would need sound, who doesn’t want a cool sci-fi door SWOOSH noise? Thanks to my new library of free sounds I found something I liked and imported it into the scene, sadly this task wasn’t as easy as dropping it into the scene and walking away to have a cuppa.

Since I’d already figured out how to add audio in the game from our lessons with the radio and by my own trial and error, I could just add in the option for audio clips on one of the scripts I already had. It seemed like the DoorOpen script (posted above) was the best one to do this on, so the sound effects would work each time the player open or closed the door. This seemed like a cleaner way of doing things, rather than trying to split up the actions among multiple scripts when I was already getting confused with the amount of scripts I had.

I had already long added my steam particle systems coming from a couple of pipes (the great things you find in the Unity store for free), however late on I realised these would be making a lot of noise if theres enough steam pressure to keep a constant escape going. So back to digging for sound effects. Once I found something suitable this was just applied to the particle system using an audio component, if the particles are running the sound will be too. Which is always because they have no script to say otherwise. Eventually I’d like to create a puzzle revolving around fixing the pipes and returning pressure to the system which may grant you access to a new part of the level.

Wrap Up

It took me two to three days to make all the art assets, it took all of half term to make it work even with help. Coding is an uphill struggle but when it works you do sit there in awe thinking ‘brilliant, I turned a light on that’s mission accomplished’!

 

Advertisements

One thought on “Walking Sim – Adding Scene Interactivity

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 )

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