- Home /
Opening a door with a key
I have got a script that works to unlock and open a door, but I was wondering if there is a way to pick up a key first to open the door and it won't work without that key. I can show you the code I have already, please let me know how I can do this.
//Player:
var doorScript : Door;
function Update () {
var hit : RaycastHit;
Debug.DrawRay(transform.position, transform.forward * 2, Color.red);
if (Physics.Raycast(transform.position, transform.forward, hit, 2)) {
if ((hit.collider.gameObject.tag == "Door") && (Input.GetButtonUp("Action") ) ) {
if (doorScript.doorState == DoorStates.locked) {
doorScript.doorState = DoorStates.unlocked;
} else if (doorScript.doorState == DoorStates.unlocked) {
doorScript.Open();
}
}
}
}
//Door Trigger:
var doorScript : Door;
function OnTriggerEnter (col : Collider) {
if ((col.gameObject.tag == "MainCamera") && (doorScript.doorState == DoorStates.unlocked) ) {
doorScript.Open();
}
}
//Door:
enum DoorStates {open, unlocked, locked};
var doorState : DoorStates;
function Awake() {
doorState = DoorStates.locked;
}
function Update () {
Debug.Log(doorState);
}
function Open() {
animation.Play("Open");
doorState = DoorStates.open;
}
Answer by BiG · Apr 24, 2012 at 05:39 PM
You have to assign a boolean variable to your Player, that says if the key has been already picked up or not. At the beginning this variable will be equal to false; When the Player enters in a collision with the key, it will turns into true. Then, you add this variable's truth as a mandatory event to open the door. Something like this:
//Player:
var doorScript : Door;
var have_key = false;
function Update () {
var hit : RaycastHit;
Debug.DrawRay(transform.position, transform.forward * 2, Color.red);
if (Physics.Raycast(transform.position, transform.forward, hit, 2)) {
if ((hit.collider.gameObject.tag == "Door") && (Input.GetButtonUp("Action") ) ) {
if (doorScript.doorState == DoorStates.locked) {
doorScript.doorState = DoorStates.unlocked;
} else if (doorScript.doorState == DoorStates.unlocked && have_key) {
doorScript.Open();
}
}
}
}
function OnCollisionEnter (theCollision : Collision){
if (theCollision.gameObject.tag == "Key")
have_key=true;
}
@BiG - you want to check this code your wrote:
if (doorScript.doorState == DoorStates.locked) {
doorScript.doorState = DoorStates.unlocked;
} else if (doorScript.doorState == DoorStates.unlocked && have_key) {
doorScript.Open();
}
Think should read:
if (doorScript.doorState == DoorStates.locked && have_key) {
doorScript.doorState = DoorStates.unlocked;
} else if (doorScript.doorState == DoorStates.unlocked ) {
doorScript.Open();
}
I can't seem to get the collision with the key to activate, any suggestions?
Answer by Fabkins · Apr 24, 2012 at 05:38 PM
Well all you need is another variable:
Player: var doorScript : Door;
var hasKey: boolean=false;
function foundAKey()
{
hasKey=true;
}
function Update ()
{
var hit : RaycastHit;
Debug.DrawRay(transform.position, transform.forward * 2, Color.red);
if (Physics.Raycast(transform.position, transform.forward, hit, 2))
{
if ((hit.collider.gameObject.tag == "Door") && (Input.GetButtonUp("Action") ) )
{
if (doorScript.doorState == DoorStates.locked && hasKey ) <-----
{
doorScript.doorState = DoorStates.unlocked;
} else if (doorScript.doorState == DoorStates.unlocked)
{
doorScript.Open();
}
}
}
}
Door Trigger: var doorScript : Door;
function OnTriggerEnter (col : Collider)
{
if ((col.gameObject.tag == "MainCamera") && (doorScript.doorState == DoorStates.unlocked) )
{
doorScript.Open();
}
}
Door: enum DoorStates {open, unlocked, locked}; var doorState : DoorStates;
function Awake() { doorState = DoorStates.locked; }
function Update () { Debug.Log(doorState); }
function Open() { animation.Play("Open"); doorState = DoorStates.open; }
Then have another object that is a key that when is picked up calls "foundAKey()".
Note this is a two sequence action as coded. The first action on the locked door unlocks it and the second action opens the door.
Give your key object a similar script that does a ray cast.
PS I would probably also not do the Raycast test this way if you had lots of doors this could get computationally expensive. I would have a player routine that does a raycast and detects what it hits. Then depending on the type of object perform the appropriate action.
I have no idea what a raycast is, and I see no reason why money is an issue. I suck at coding, so i'm not exactly sure how to go about doing that
Well dont worry about it for now. Get something working and optimise later. Enjoy :)
Your answer
Follow this Question
Related Questions
Opening a door with a key 0 Answers
How to open a gate with a key ? 1 Answer
Help with making his work with a key 1 Answer
Scripting issue with opening a door with a key. 2 Answers
Open door with key 2 Answers