Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 f1shhy · Mar 08, 2013 at 08:46 PM · javascriptinventorydoorkey

Locked door and key to open it script

I got a key and a door. The door is locked and should be opened only when you found the key.

this is the code I got for the key and the door. (2 separate codes)

 //Door script
 
 private var open : boolean = false;
 
 var openDoorAnimationString : String;
 var closeDoorAnimationString : String;
 
 var buttonTransform : Transform;
 var distToOpen : float = 6;
 
 private var playerTransform : Transform;
 private var cameraTransform : Transform;
 
 var openDoorSound : AudioClip;
 var closeDoorSound : AudioClip;
 
 function Awake () {
     playerTransform = GameObject.FindWithTag("Player").transform;
     cameraTransform = GameObject.FindWithTag("MainCamera").transform;
     if(open)
         animation.Play(openDoorAnimationString);
 }
 
 function Update () {
     var alreadyChecked : boolean = false;
     if (Vector3.Distance(playerTransform.position, buttonTransform.position)<= distToOpen)
     if (Input.GetKeyDown("e") && !animation.isPlaying)
     {
         if(open)
         {
         animation.Play(closeDoorAnimationString);
         open = false;
         alreadyChecked = true;
         if(closeDoorSound)
             audio.PlayOneShot(closeDoorSound);
         }
         if(!open && !alreadyChecked)
         {
         animation.Play(openDoorAnimationString);
         open = true;
         if(openDoorSound)
             audio.playOneShot(openDoorSound);
         }
     }
 }
 
 
 
 
 
 
 
 
 //Key Script
 #pragma strict
 private var open : boolean = false;
 
 var pickupSound :AudioClip ; //when picking up the key the sound
 var audioPos :Vector3 ;
 
 var liftKeyAnimationString : String;  
 var lowerKeyAnimationString : String;
 
 var buttonTranform : Transform;
 var distToOpen : float = 6;
 
 private var playerTransform : Transform;
 private var cameraTransform : Transform;
 
 function Awake ()
 {
     playerTransform = GameObject.FindWithTag("Player").transform;
     cameraTransform = GameObject.FindWithTag("MainCamera").transform;
     if(open)
         animation.Play(liftKeyAnimationString);
     
 }
 
 function OnTriggerEnter(Collider)
 {
     AudioSource.PlayClipAtPoint(pickupSound, audioPos);
     Destroy(gameObject);
     print("You got the Key");
 }
 
 function Update () 
 {
     var alreadyChecked : boolean = false;
     if (Vector3.Distance(playerTransform.position, buttonTranform.position)<= distToOpen)
     if (Input.GetKeyDown("e") && !animation.isPlaying)
     {
         if(open)
         {
             animation.Play(lowerKeyAnimationString);
             open = false;
             alreadyChecked = true;
         }
         if(!open && !alreadyChecked)
         {
             animation.Play(liftKeyAnimationString);
             open = true;
         }
     }
     transform.Rotate(0, 50 * Time.deltaTime, 0);
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hyperion · Mar 08, 2013 at 10:46 PM

If you want the game to know whether you've gotten a key, make a variable like

var HaveKey: boolean

and when you 'pick up' the key, set HaveKey to true. Then, upon collision with the door, if you press the open door button and HaveKey is true, you can open it.

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.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Key Item in C# 1 Answer

Open doors with key & sound 1 Answer

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

Key to open door not working. 1 Answer

how do i do a simple key door 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