Key Item Pickup, Inventory and Door Open v2 scripts

We needed to sort out some kind of inventory system so that the player could find objects and pick up key items. I tried for a ling time to create my own script but after many failed attempts I was able to find an inventory system script on the unity asset store.

Inventory system

preeE

After doing some setting up I was able to treat this circle as a ‘key object’. When E is pressed the object is destroyed and a global Boolean variable is flipped which allows the player to open the main door.

inventory

You can also hold down the tab button to display the inventory, I created this key sprite so you can identify when you have the key or not.

The code is far too long for me to post here (you can download it + the assets pack here: http://www.mediafire.com/download/w9p98xrg2u9j5tt/InventoryScripts+Assets.zip) but after editing the code and applying it to my door script I was able to create this message that is displayed when you get close to the door without a key.

youneedakey

Door Open/Close Script + Animation

We needed doors that the player can open so that certain areas could be blocked at the start so I made a rectangle in maya and animated it opening.

door3door4

Then I imported the door into unity and set up two animation takes so that I had two different states for the door being closed and opening. Essentially one was a 1-frame animation and the other was a 28-frame animation.

doortakes

I did this so I had control over the two states and I could reference them when it came to coding.

door1

After setting all this up the next step was to get the door to open with coding:

//put this script on both door and door trigger
var DoorGameObject : Transform; // This is a reference to the door GameObject that has your animation
private var HasTriggerBeenUsed : boolean = false; // This is to make sure that the button is not repeatedly pressed.
private var doorOpen : boolean = false;
public var myFirstPersonPickUp : FirstPersonPickUp;
public var DoorTrigger : GameObject;
public var CanOpen : boolean = false;
function OnTriggerEnter(otherObj: Collider)
{
{
Debug.Log("Can Open With Key + E");
CanOpen = !CanOpen;
}
}
function OnTriggerExit (otherObj: Collider)
{
{
Debug.Log("Cannot Open");
CanOpen = false;
}
}
function Start ()
function Update () {
if (Input.GetKeyDown("e") && HasTriggerBeenUsed == false && CanOpen == true) {
DoorGameObject.animation.Play("Doormove");
HasTriggerBeenUsed = !HasTriggerBeenUsed;
Debug.Log("Door open");
}
else if (Input.GetKeyDown("e") && HasTriggerBeenUsed == true && CanOpen == true) {
Debug.Log("Can't open door.");
}
}

This code basically says, if the E button is pressed within the trigger area and it hasn’t been opened before, it will open and play the door movement animation.

door2

Flashlight + Respawn Scripts

Since our game environment is set in a post apocalyptic environment, we figured a useful game mechanic to have to be a torch seeing as electricity and light would be quite scarce meaning there would be a lot of dark areas, which can be an interesting game mechanic depending on the genre of game. This is seen a lot in horror games.

(Screenshot from The Evil Within, 2004)

(Screenshot from The Evil Within, 2004)

I approached this by creating a spot light and attaching it to the first person controller at an angle, as shown above.

light5

Then I attached a flashlight script to the spot light.

light4

I wanted the script to work like this:

Get right click input > toggle spot light on/off

By using this script I was able to do this successfully:


var FlashLightOn : boolean = false;
function Update()
{ // Right click = toggle flashlight
if(Input.GetButtonUp("Fire2"))
{
if(FlashLightOn==true)
{
GameObject.Find("FlashlightLight").light.enabled=true;
FlashLightOn=false;
}
else
{
GameObject.Find("FlashlightLight").light.enabled=false;
FlashLightOn=true;
}
}
}

Final result:

output_RIp7MF

Since the game is set on the rooftop people are bound to fall off at some point if there aren’t any barriers like railing or fences set up to prevent this. This is why I made this simple respawn code.

public var deathHeight : float = -5;
public var lives : int = 3;

function spawn()
{
if(transform.position.y <= deathHeight);
	{
	transform.position = Vector3(4, 1, 0);//change in full game to new respawn point
	}
}

This code is attached to the player and states, ‘If the player’s Y-value (number determining vertical position) goes below a certain number, it will transport the player back to the original position at the Vector3 values.

I kept the lives variable in just in case we decide to add the concept of lives to our final rooftop scene.