Programming – Self Taught Feature

We have been asked to add a feature to one of the many games we’ve worked on that has been discovered through self tuition and the Unity documentation. This has never been my strongest area so I thought it would be best to keep things simple.

In my walking simulator I had a locked door on the bottom floor with an inaccessible room behind it, I had originally planned to make some combination of a mis-functioning door/jump scare. Working with this I thought I could add a key card reader, so a panel that would interact with a key, give an indication the key has been accepted with a change of light and then begin to open the door with a failure.

Using the Renderer in Unity was one of the recommendations in class for things to look at, and I’ll be needing use of it to complete this feature.

doorbutton4

I started (in a new script) by declaring the variables I’d need:

  • public string keyItemName – Has seen use before and allows me to type in an item name which can be searched for in the inventory list.
  • public GameObject light – At some point I’d need to affect a game object I’ll be changing the material of, in this case I named it light.
  • public Material lightOff – To allow me to set the material for the off state.
  • public Material lightOn – To allow me to the material for the on state.
  • public GameObject door – Once the lights had been changed, I wanted the nearby door to open.
  • Renderer rend – Declaring we’d need to make use of the renderer and called it rend, also a private variable as nothing else will need to use it.

doorbutton2

Here is how it looks in the inspector, after being attached to the object in the scene which would be used as a button to trigger the script. The item required has been input, which game object will be our light, on and off materials have been setup and input and the door declared.

doorbutton5

Continued on in the script, we use existing script to access the inventory controller and look for the KeyItemName (declared above). If the item is found the script goes looking for the renderer component for the variable light, which will point to our physical game object (in the actual scene this is just a sphere).

doorbutton3

We’re then asking the renderer to change the material for the object to what has been declared in lightOn variable. You can see above how the renderer for light looks in the inspector, we’re asking the script to change the entry in Element 0.

Finally using old iTween code, we tell the door to raise in the Y direction. Now all my other doors are raised by 3 units which grants a full open, I already mentioned I wanted the door to malfunction so only raised the door half height (1.5f) to make it inaccessible to the player. The script then destroys itself so the door can’t be raised further by the player continuing to press the button.

Here is how it looks when played:

lightsdemo

Technically we didn’t need to declare the lightOff material as the light never returns to its original state. However I could modify the other doors in future to all have open/closed indicators and wanted to leave the variable in for this reason.

 

Advertisements

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