Walking Sim

Today I have been working on more code on the walking sim game.

I have been working on the code for inventory and the door…

 

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

public class Inventory : MonoBehaviour {

public List<string> items = new List<string>();
}

 

This code allowed me to have an inventory for the game, this means that i will have a string of items acting as an inventory.

using UnityEngine;
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);
// Destroy (pick up) this item
Destroy(gameObject);
}
}

This code allows me to pick up the key by clicking the object, this then destroys the item from the scene to show that I picked it up.

using UnityEngine;
using System.Collections;

public class DoorController : MonoBehaviour {

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

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
transform.Rotate(0,80,0);
// Destroy this script
Destroy(this);
}
}
}

This code makes the door open when the key is picked up, the code says that when the key is picked up the door will open at a certain angle and will open for the player to go through.

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