- Home /
Key and Door Tutorial Help?
I was following a tutorial to make a key open a door to lead to the end of the game/the next level, but I can't physically pick up the key to an inventory and therefore I can't open the door. These are the scripts I'm using:
Door Script -
var Key : GameObject;
private var Door = false;
function Start ()
{
}
function Update ()
{
if (Input.GetKeyDown(KeyCode.E) && Door == true && Key.active == false)
{
Application.LoadLevel("NextLevel");
}
}
function OnTriggerEnter (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
Door = true;
}
}
function OnTriggerExit (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
Door = false;
}
}
Key Script -
var TheKey : GameObject;
private var playerNextToKey = false;
function Update ()
{
if (Input.GetKeyDown(KeyCode.E) && playerNextToKey == true)
{
TheKey.active = false;
}
}
function OnTriggerEnter (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
playerNextToKey = true;
}
}
function OnTriggerExit (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
playerNextToKey = false;
}
}
Please can someone tell me how to physically pick up the key so that I can then unlock the door to either end the game (preferably) or lead to the next level.
HELP NEEDED ASAP, THIS GAME HAS A DEADLINE!
Answer by Maui-M · Feb 06, 2014 at 09:03 PM
The script looks fine. I think you need to change the tag on your main character object to "Player".
Answer by geroppo · Feb 06, 2014 at 10:05 PM
If you just need an easy and fast way to make an inventory of keys, create an inventory script like so
public class Inventory
{
List<Key> keys=new blabla;
public bool hasKey(Key key)
{
// And here just check if the received key( from the
// door or something else) is in the inventory.
return true;
}
}
then in the door you can make something like :
void Update()
{
if(Input.GetKeyDown(KeyCode.E)&& Door)
{
//get a reference to the player that has the inventory, and then
if(player.GetComponent<Inventory>().haskey(key))
{
//load level
}
}
}
this is the fast and easy way, i wouldn't do it this way but since you are in a hurry ,this will do i suppose