Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cycoclash25 · Apr 24, 2012 at 05:23 PM · doorkeyopenwith

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;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
 
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fabkins · Apr 24, 2012 at 05:47 PM 0
Share

@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();
         }
avatar image cycoclash25 · Apr 24, 2012 at 05:49 PM 0
Share

Okay, thanks, i'll give it try

avatar image cycoclash25 · Apr 25, 2012 at 04:10 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fabkins · Apr 24, 2012 at 05:42 PM 0
Share

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.

avatar image cycoclash25 · Apr 24, 2012 at 05:47 PM 0
Share

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

avatar image Fabkins · Apr 24, 2012 at 05:49 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Opening a door with a key 0 Answers

key to open gate 1 Answer

Press an in-game switch to open a door - help? 2 Answers

GUI text, door and object collider 2 Answers

Open door with key 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges