x


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;
}
more ▼

asked Apr 24 '12 at 05:23 PM

cycoclash25 gravatar image

cycoclash25
-24 5 6 6

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;

}
more ▼

answered Apr 24 '12 at 05:39 PM

BiG gravatar image

BiG
4.7k 4 13 49

@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();
        }
Apr 24 '12 at 05:47 PM Fabkins

Okay, thanks, i'll give it try

Apr 24 '12 at 05:49 PM cycoclash25

I can't seem to get the collision with the key to activate, any suggestions?

Apr 25 '12 at 04:10 PM cycoclash25
(comments are locked)
10|3000 characters needed characters left

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()".

more ▼

answered Apr 24 '12 at 05:38 PM

Fabkins gravatar image

Fabkins
675 15 20 26

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.

Apr 24 '12 at 05:42 PM Fabkins

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

Apr 24 '12 at 05:47 PM cycoclash25

Well dont worry about it for now. Get something working and optimise later. Enjoy :)

Apr 24 '12 at 05:49 PM Fabkins
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x193
x166
x95
x19

asked: Apr 24 '12 at 05:23 PM

Seen: 916 times

Last Updated: Apr 25 '12 at 04:10 PM