Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TheGoliath · May 03, 2012 at 01:00 AM · doorpickupkeyunlock

Problems with key script

I have a script attached to a key, that's supposed to see if the player has entered the trigger AND if they have pressed a key. Then it's supposed to allow me to open a door that I couldn't open before I got the key. But I haven't even been able get past the part where I have to press the key when I'm within the trigger, because nothing happens when I do. "Picked Up Key" is supposed to be printed to the console and the key is supposed to be destroyed. But neither of those happen. The first script here is one I've modified to my own needs from a different question:

Key Script

 private var gotKey : boolean = false;
 var player : GameObject;

 function Start() {
     player = GameObject.Find("lindamayfield");
     return gotKey;
 }

 function OnControllerColliderHit (hit: ControllerColliderHit) {
 // ...

 if (Vector3.Distance(transform.position, player.transform.position) < 3)
 if (Input.GetButton( "Examine" ))
     print("Picked Up Key");
     gotKey = true;
     Destroy (gameObject);    
 }

Door Script

 //Instruction:
 //Make an empty game object and call it "Door"
 //Rename your 3D door model to "Body"
 //Parent a "Body" object to "Door"
 //Make sure thet a "Door" object is in left down corner of "Body" object. The place where a Door Hinge need be
 //Add a box collider to "Door" object and make it much bigger then the "Body" model, mark it trigger
 //Assign this script to a "Door" game object that have box collider with trigger enabled
 //Press "f" to open the door and "g" to close the door
 //Make sure the main character is tagged "player"

 // Smothly open a door
 var smooth = 2.0;
 var DoorOpenAngle = 90.0;
 var DoorCloseAngle = 0.0;
 var open : boolean;
 var enter : boolean;
 var Locked : AudioClip;
 var Unlock : AudioClip;

 //Main function
 function Update (){

 if(open == true){
 var target = Quaternion.Euler (0, DoorOpenAngle, 0);
 // Dampen towards the target rotation
 transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
 Time.deltaTime * smooth);
 }

 if(open == false){
     var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
 // Dampen towards the target rotation
 transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
 Time.deltaTime * smooth);
 }

 var Key1 = GameObject.Find("lindamayfield").GetComponent("PickuUp");    
 
 if(enter == true){
 if(Input.GetKeyDown("return")){
     audio.clip = Locked;
     audio.Play();
 }
 }
 
 if(enter == true){
 if(Key1.gotKey) {
 if(Input.GetKeyDown("return")){
     open = !open;
     audio.clip = Unlock;
     audio.Play();
     }
     }
 }
 }
 //Activate the Main function when player is near the door
 function OnTriggerEnter (other : Collider){
 
 if (other.gameObject.tag == "Player") {
     (enter) = true;
  }
 }

 //Deactivate the Main function when player is go away from door
 function OnTriggerExit (other : Collider){
 
 if (other.gameObject.tag == "Player") {
     (enter) = false;
  }
 }
 //@youtube.com/user/maksimum654321

The door script I actually got from a youtube video, not Unity Answers. But I still attempted to modify it, despite me still being a beginner at scripting. The door makes the "locked" sound when I try to open it without getting the key first, which is obviously what I want. But that's as far as I've gotten, because I can't even pick up the key...

Comment
Add comment · Show 1
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 TheGoliath · May 03, 2012 at 04:46 PM 0
Share

Can someone please help me with this? I really need to get it done.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unlocking Gate with Key 1 Answer

Door Open With Object Pickup 1 Answer

How to open a gate with a key ? 1 Answer

Help with making his work with a key 1 Answer

Trying the get a Pickup key script working, need some help. 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