Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by BlackKrow · Apr 04, 2016 at 07:55 AM · keyactivationdoorsunlock

How to make this door open once the key has been walked over?

I'm trying to make a door open once the key has been picked up/destroyed. I'm not good with scripted yet so I need some help.

Door Code:

     public class OpenCafeteria : MonoBehaviour {
 
     public GameObject Doorknob;
     public GameObject Objective2Complete;
         
     public GameObject doorHinge;
 
     private Animator hingeAnim;
 
     private bool isOpen;
 
     public AudioSource controlAudio;
 
     public AudioSource controlAudio2;
 
     
 
 
     // Use this for initialization
     void Start()
     {
         hingeAnim = doorHinge.GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void Update()
     {
         isOpen = hingeAnim.GetBool("toggleOpen");
     }
 
     void OnTriggerEnter(Collider entity)
     {
         if (entity.tag == "Player")
         {
             print("The Player has entered the door.");
         }
     }
 
     void OnTriggerStay(Collider entity)
     {
         if (entity.tag == "Player")
         {
             if (Input.GetKeyUp(KeyCode.E) && isOpen == false)
             {
                 hingeAnim.SetBool("toggleOpen", true);
                 controlAudio.Play();
                 print("The Player opened the door.");
             }
             if (Input.GetKeyUp(KeyCode.E) && isOpen == true)
             {
                 hingeAnim.SetBool("toggleOpen", false);
                 controlAudio2.Play();
                 print("The Player closed the door.");
             }
         }
     }
 
     void OnTriggerExit(Collider entity)
     {
         if (entity.tag == "Player")
         {
 
             print("The Player has left the door.");
         }
     }
 }


And Here is the Key code:

 public class Objective2Complete : MonoBehaviour {
 public GameObject ObjectiveListManager;
     public AudioSource controlAudio;
     public bool objTwoComplete = false;
 
     // Use this for initialization
     void Start ()
     {
       
     }
         
     void OnTriggerEnter(Collider entity)
     {
         print("Player picked the Cafeteria Key");
         if (entity.tag == "Player")
         {
             objTwoComplete = true;
             controlAudio.Play();
             Destroy(gameObject);
             ObjectiveListManager.GetComponent<ObjectiveList>().CompleteObjective(2);
             print("Objective 2 completed");
         }
         
    } 
     // Update is called once per frame
     void Update ()
     {
     
     }
 }

If it's not too much trouble, could you ELI5 why the lines of code you added are needed? So I don't have this problem again in the future.

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 ZefanS · Apr 06, 2016 at 02:38 AM 0
Share

Does this code successfully make the door open?

 if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.E) && isOpen == false)
 {
     hingeAnim.SetBool("toggleOpen", true);
     controlAudio.Play();
     print("The Player opened the door.");
 }

If so, then you just have to do basically the same thing when the player gets the key. Based on your scripts, you should have all the knowledge you need to do this.

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

54 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Manual activation error - Hwid ulf can't be blank 0 Answers

I have a error in a simple script 0 Answers

S key always pressed 0 Answers

How to do Unlockable Items for a Character Collection Clicker Game? How to disable buttons? 0 Answers

how to run unity 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