• 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 Banana13 · May 28, 2013 at 08:08 PM · doorkeyopenkeys

How to make 2 doors with 2 diffrent keys

I have a door w$$anonymous$$ch I can open with a key. But I wanna make 2 doors with 2 diffrent keys. Here are the Scripts.

T$$anonymous$$s one is the Script on the door: //Booleans: var IsOpen : boolean = false; var smooth = 2.0; var DoorOpenAngle = 90.0; var CanOpen : boolean = false; var NeedKey : boolean = true; private var enter : boolean; //Audios: var Locked : AudioClip; var Open : AudioClip; var Close : AudioClip; //Other: var KeyScript : GameObject; private var openRot : Vector3; private var defaultRot : Vector3; function Update () { if(Input.GetKeyUp(KeyCode.E) && !IsOpen && CanOpen && !NeedKey) { Opening(); IsOpen = true; audio.clip = Open; audio.PlayOneShot(Open); } else if(Input.GetKeyUp(KeyCode.E) && IsOpen && CanOpen && !NeedKey) { Closing(); IsOpen = false; audio.clip = Close; audio.PlayOneShot(Close); } else if(Input.GetKeyUp(KeyCode.E) && CanOpen && NeedKey) { audio.clip = Locked; audio.Play(); } if(KeyScript.GetComponent(Key).HaveKey == true) { NeedKey = false; } } function Start(){ defaultRot = transform.eulerAngles; openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z); } function Opening() { for (var i = 0; i < 35; i++ ) { transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth); yield WaitForSeconds(0.01); } } function Closing() { for (var i = 0; i < 35; i++) { transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth); yield WaitForSeconds(0.01); } } function OnTriggerEnter (other : Collider) { if(other.gameObject.tag == "Player") { CanOpen = true; } } function OnTriggerExit (other : Collider) { if(other.gameObject.tag == "Player") { CanOpen = false; } } @script RequireComponent(AudioSource)

T$$anonymous$$s Script is on the MainCamera: var ShowText : boolean = false; var Text : String = "You picked up a key"; var other : GameObject; function Update () { var $$anonymous$$t : RaycastHit; var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0)); var fwd = transform.TransformDirection (Vector3.forward); Debug.DrawRay(transform.position, fwd * Length, Color.green); if(Physics.Raycast(ray, $$anonymous$$t, Length)) { $$anonymous$$tObject = $$anonymous$$t.collider.gameObject; if($$anonymous$$tObject.gameObject.tag == "Key") { other.GetComponent(Crosshair).TextureChange(); if(Input.GetKeyUp(KeyCode.E)) { GetKey(); Destroy($$anonymous$$tObject); } } else if($$anonymous$$tObject.gameObject.tag == "Untagged") { other.GetComponent(Crosshair).BackToNormal(); } } else if(!Physics.Raycast(transform.position, transform.forward, $$anonymous$$t, Length)) { other.GetComponent(Crosshair).BackToNormal(); } } function GetKey(){ HaveKey = true; ShowText = true; yield WaitForSeconds(3); ShowText = false; } function OnGUI(){ if(ShowText) { GUI.Label(Rect(Screen.width /2 -62.5, Screen.height /2 + 50, 200, 100), Text); } }

And t$$anonymous$$s Script is on the MainCamera too:

     var Length : float = 3;
      
     static var HaveKey : boolean = false;
     var ShowText : boolean = false;
      
     var Text : String = "You picked up a key";
      
     var other : GameObject;
      
     function Update () {
      
     var $$anonymous$$t : RaycastHit;
            
     var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
            
     var fwd = transform.TransformDirection (Vector3.forward);
      
     Debug.DrawRay(transform.position, fwd * Length, Color.green);
            
             if(Physics.Raycast(ray, $$anonymous$$t, Length))
      
             {      
                    
                     $$anonymous$$tObject = $$anonymous$$t.collider.gameObject;
                    
                     if($$anonymous$$tObject.gameObject.tag == "Key")
      
                             {
                    
                                     other.GetComponent(Crosshair).TextureChange();
                    
                                     if(Input.GetKeyUp(KeyCode.E))
                                     {
                            
                                             GetKey();
                                             Destroy($$anonymous$$tObject);
                                            
                            
                                     }
                                    
                                    
                             }
                    
                             else if($$anonymous$$tObject.gameObject.tag == "Untagged")
                    
                             {
                                     other.GetComponent(Crosshair).BackToNormal();
                             }
      
                     }
            
             else if(!Physics.Raycast(transform.position, transform.forward, $$anonymous$$t, Length))
             {
                     other.GetComponent(Crosshair).BackToNormal();
             }
            
            
      
     }
      
     function GetKey(){
      
             HaveKey = true;
      
             ShowText = true;
            
             yield WaitForSeconds(3);
            
             ShowText = false;
     }
      
     function OnGUI(){
      
     if(ShowText)
     {
             GUI.Label(Rect(Screen.width /2 -62.5, Screen.height /2 + 50, 200, 100), Text);
     }
      
     }`





The last scipt is for Crosshair. How can I make 2 diffrent keys for 2 diffrent doors now ?

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
0
Best Answer

Answer by darthbator · May 28, 2013 at 08:53 PM

You can even just check the gameObject itself. So store a reference to the gameObject of the key you want to use and then check if player.heldKey == requiredKey. That's probably how I would handle t$$anonymous$$s. If you need more granular or accounting control you could wind up the keys and doors in hashrefs and then specifically "key" them together with strings.

Comment
Add comment · Show 1 · 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 Banana13 · May 29, 2013 at 06:38 AM 0
Share
avatar image
2

Answer by Stormizin · May 28, 2013 at 08:13 PM

Set a ID for a key and w$$anonymous$$ch door the key open.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scripting issue with opening a door with a key. 2 Answers

Door opening with a key, my scripts and problem 1 Answer

Script for a door that requires (9 papers) to open 0 Answers

Make doors open with different keys? 1 Answer

Door script lag with rotation (Quaternion.slerp ?) 0 Answers


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