- Home /
Simple pickup key and unlock door
Hi, I'm relatively new at coding in unity and I've been looking for over a week now but I would like to know if anybody has any idea on how to code a simple code that will allow a player to pick up a key object and then once the key has been picked up, unlock a specific door. I've looked at both javascript and c# but I don't seem to be having any success. Any help will be appreciated :D
Answer by Yofurioso · Mar 18, 2014 at 12:53 PM
This tutorial may help: http://www.youtube.com/watch?v=4l2YJsgmusI
It explains how to pick up items, then you just need to have a boolean variable to know if your player is holding the key, and if he is, open the door.
Answer by Calum-McManus · Mar 18, 2014 at 12:27 PM
This website really isn't for handing out code samples, but I'll give you some tips on where you can start so you can learn to code not just leach of others.
First you will want a RayCast to your mouse position or centre of you screen (depending on how you game works)
Have the RayCast check what it's hitting when you click (Input.GetMouseButtonDown).
Then if the ray cast hit an object with the tag "Key", destroy the key and make a boolean (e.g. private bool GotKey) true, then check if the ray cast is hitting an object with the tag "Door", if it is and "GotKey" is true, Open door.
If you really can't figure it out I can provide you with the script (in C#) but that will not help you learn at all.
Mainly research RayCasts, RayCastHits and If statements, its all you need to make this script.
Hey Calum.$$anonymous$$c$$anonymous$$anus I've been trying for a while now but I'm not sure I'm on the right idea as I keep getting complier errors and so I can't see if the code actually works. I have two codes, one to go onto the door that needs to be unlocked and on the would go on the player I'm guessing.
Here's the key script: #pragma strict
public var $$anonymous$$eyGot : boolean = false;
var hit : RaycastHit;
function Update () {
if (Physics.Raycast (transform.position, transform.forward, hit, 1)){
(Input.Get$$anonymous$$ouseButtonDown && (hit.collider.tag("key")));
Destroy(GameObject.FindWithTag("key"));
$$anonymous$$eyGot = true;
}
}
And here's the door script: var smooth = 2.0; var DoorOpenAngle = 90.0; var DoorCloseAngle = 0.0; var open : boolean; var enter : boolean;
function Update (){
if(open == true && $$anonymous$$eyGot == true){
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth);
}
if(open == false && $$anonymous$$eyGot == false){
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, target1, Time.deltaTime * smooth);
}
if(enter == true){
if(Input.Get$$anonymous$$ouseButtonDown(0)){
open = !open;
}
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
(enter) = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player"){
(enter) = false;
}
}
If you can help it would be much appreciated :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613140539im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to create a basic follow AI 1 Answer
Script for collectiong keys to escape 2 Answers
looking for help with item pickups 1 Answer
Inventory system? 3 Answers
each enemy have different damage amount, how to calculate the hp remain??? 1 Answer