- Home /
Door and Key System please help
Hello everybody, I'm new to Unity and Scripting in general. I need a Door&Key System, But I can't figure out how to write it. I want the player to find a key, then go in a location (room) and load next level (such as a key for a teleporter or something like this)
Obviously without the key the player can't access the next scene. Can you please help me? Thanks in advance and sorry for my bad english.
You could try attaching this to the player:
var Has$$anonymous$$ey : boolean;
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.name == "Door") { //Or gameObject.tag == "Door" it would be more safe way of doing it if you have a door which doesn't do anything
if (Has$$anonymous$$ey) {
Application.LoadLevel(1); //Load level 2
}
}
}
This is good but I also need a key script and an inventory script, qhit this script I tell the door "If the player with the key collides with you, load the next level" But I need a "Has$$anonymous$$ey script" and an Inventory script. Also a Pickup$$anonymous$$ey Script..... :D
You need to do some reading and learning, you might as well ask us to write your game for you. Inventory scripts can be lengthy...
I don't want you to write my game, I probably asked it in a bad way :D I just need an Idea to figure out how to write a full-working script. :D Sorry for all these smiles :D Can't stop :D O$$anonymous$$G D:
Answer by Dimling · Oct 23, 2014 at 01:08 PM
I wont write a lot of scripts for you but i can give you an idea of how this could be done.
The inventory could be a list of gameobjects.
List<GameObject> listOfGameObjects= new List<GameObject>();
When you find a key, for instance OnCollision with key, you can add this gameobject to the list, and maybe delete it from the scene.
void OnCollisionEnter(Collision objectYouHit){
if (objectYouHit.gameObject.name == "Key") {
listOfGameObjects.add(objectYouHit);
Destroy(objectYouHit);
}
}
When you come to the door, you can check the list if there is a key object there, and then use the script that extreme powers presented above to open the door!
Since you are new to Unity and scripting I would advice you to have a look at some tutorials. I would suggest the following youtube videos made by the tornardo twins. Its an easy introduction to unity and a good way to learn some simple scripting!
Unity has many of its own tutorials too... http://unity3d.com/learn/tutorials/modules
True! I've just submitted the one i had the best experience with! :)
Answer by richyrich · Oct 23, 2014 at 01:06 PM
1) Visit this location for more info on getting the key and door to work
http://answers.unity3d.com/questions/244159/opening-a-door-with-a-key.html
2) Making sure there is a collider on the door and is set up as a trigger, place the following code inside the OnTriggerEnter function... Application.LoadLevel(nextLevel);
Obiviously you need set nextLevel = 2; or something somewere higher up in your program
Answer by Sarthak123 · Oct 23, 2014 at 01:55 PM
you can use a very simple trick, that is GameObject.SendMessage and OnTriggerEnter() so first tag your player with the tag "Player" to begin, create a key and a door, make them close just for testing this script quicly and fast.. you can write a script like this for your key,(assuming your key is collectible object like a rotating object in floor) you have to attach an collider which is set to trigger to both the door and your key for these scripts to work.
var door : GameObject; // door in the scene
function OnTriggerEnter(other : Collider)
{
if(other.tag == "Player") // to check if player has entered or not.
{
door.SendMessage("Open"); // sending message to door to open
Destroy(gameObject); // destroying key since it has been collected.
}
}
next you need to attach a script to your door which initiates the message "Open" sent by the key...
var canOpen : boolean = false;
function Open() // will be initiated on recieving message by the key.
{
canOpen = true; // tell the door that the key has been collected and now it can be opened if only player reaches to the door, we will check whether the player has reached near the door or not in the next step.
}
function OnTriggerEnter(other : Collider)
{
if(other.tag == "Player" && canOpen) // player has entered and door can be opened or not?
{
// do something
// here write your script how you would open your door, ex. by playing an animation, etc.
}
}
this method will definitely work and you can check script referrence for GameObject.SendMessage and OnTriggerEnter for better understanding..!
hope it helps you in your game.
Thanks this is useful. Now, i the door script at the end, where you wrote the do something comment etc. Can I just simply write Application.LoadLevel(2); ? (loads the second level in the build settings. will that work?
Thanks $$anonymous$$an :D It worked, I just changed something in the door script, If you try to open without the key a tetx displays and a sound plays, It works perfectly, THAN$$anonymous$$S $$anonymous$$AN :D
no problem, please accept my answer, i will be glad to see that..:)
@$$anonymous$$C_Shadows i meant mark my answer as correct, u just voted my comment...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help making an audio trigger. 2 Answers
How can I teleport objects. 4 Answers
Glitchy Turret Script 2 Answers
Problem with OnTriggerEnter() 1 Answer