- Home /
The question is answered, right answer was accepted
Pickable item to unlock doors (Being able to proceed next scene)
Hi everyone, I'm currently working on a horror game and after hours of building my scene, I got stuck with one little thing: Picking up an key and being able to unlock a door to change scene, and if I don't have a key, I want to restrict the action of changing scenes.
For now on, my key already has a Sphere collider with the "Is Triggered" checked.
Here's a 2 screenshots of my Key and the door:
Key:
Door:
If you guys have a good way to solve this question or if you need more info, please post them below! I will reply your answers/comments quick and fast.
Thanks!
~crusherxman
I was looking for a simple key code, this worked perfect :-)
Answer by ExTheSea · May 04, 2013 at 03:11 PM
I will try to make an example code which hopefully will work in your case:
var hasKey = false;
function Update(){
if(Input.GetKeyDown(KeyCode.E)){ //If E is pressed
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, 50)) { // Sends out a raycast to check if something is in front of you
if(hit.transform.tag == "Key"){ // If the object in front of you has the tag Key
Destroy(hit.transform.gameobject);//Destroy Key Object
hasKey = true;
}else if(hit.transform.tag == "Door"){ //Checks if the gameobject you're looking at has the tag Door
if(hasKey)
hit.transform.SendMessage("Unlock"); //Calls the function Unlock on the door
}
}
}
}
Add this script to your camera, Add a function Unlock to the door which actually opens the door or removes a lock, .... and add the tag Door to the door and Key to the key. Or you change the code to match your case.
Hope it works for you if not let me know.
Have you checked if the script was valid? I came up with 5 errors...
No i actualy did not i was writing it here and not inside unity. I will check.....Ok i forgot a bracket after Update() i corrected my code and also fixed a bug in the raycast. The only thing was that i didn't get the Destroy line to work for some reason but maybe it works in your case.
Doesn't work, although what you mean about my door's function? Like changing scenes?
Well my script calls a function called Unlock a script attached to the Door gameobject. So if you want to change the scene the second you unlock the door you have to put your level-change code inside an Unlock function and attach a script containing this function to the Door.
Did you get any other errors except maybe that the function Unlock wasn't found or something like this?
I haven't got errors but yes I'm missing a Unlock function...
Answer by fafase · May 13, 2013 at 04:23 PM
So your key has a sphere collider set to IsTrigger.
On your player you have:
var hasKey:boolean = false;;
function OnTriggerEnter(other:Collider){
if(other.collider.tag == "Key"){
hasKey = true;
Destroy(other.gameObject);
}
}
function Update(){
var hit :RaycastHit;
if(Pyhsics.Raycast(transform.position, transform.forward, hit, 2)){
if(hit.collider.tag == "Door" && hasKey && Input.GetKeyDown(KeyCode.Space)){
Application.LoadLevel("nextLevel");
}
}
}
Hopefully I did not miss anything...The idea is simple, if you collect the key you get the boolean and you need the boolean to open the door.
EDIT:
I actually, I realize I just reformulated the other answer already given...
I'm getting this error:
Assets/Has$$anonymous$$ey_Script.js(1,7): BCE0044: expecting EOF, found 'has$$anonymous$$ey'.
I don't know if you actually did tried or tested it, but it just needs a few changes (I've tried myself and it didn't work)
@crusherxman : Did my answer not work? I mean fafase already said it. It's pretty much the same thing as my answer.
I just put the necessary scripts in a new project and packed it in a zip file.
Load the TestPickup scene and start it. Then press E when looking at the right cube and then press E on the left cube. You should be transfered to a scene called Scenetoloadafterunlock.
If that works you can adapt the setup in your project.
https://dl.dropboxusercontent.com/u/83937500/TestPickup$$anonymous$$ey.zip
Follow this Question
Related Questions
Door rotate on hinge 1 Answer
Playing animation when picking up an object. 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Endless DeathZone??!!? 4 Answers