Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 psych77 · Mar 21, 2017 at 01:48 AM · c#dooropendoors

Opening door with the same key?

Hey guys, i am struggeling a bit with opening doors with the same key.

For now im using a script that opens with E and closes with F. It also contains sound and text on trigger. Any ideas what im missing?

//Script made by Strups Productions// using UnityEngine; using System.Collections;

public class doorOpenandClose : MonoBehaviour {

 //Text variables
 public bool T_ActivatedOpen = true;
 public bool T_ActivatedClose = false;
 public bool activateTrigger = false;

 public GameObject textO;
 public GameObject textC;

 //Animator variables
 Animator animator;
 bool doorOpen;
 //Sound variables
 public GameObject doorOpenSound;
 public GameObject doorCloseSound;






 void Start () { //what happens in the beginning of the game.
     textC.SetActive (false);
     textO.SetActive (false);
     T_ActivatedOpen = true;
     T_ActivatedClose = false;

     animator = GetComponent<Animator> ();
     doorOpen = false;


     doorCloseSound.SetActive (false);
     doorOpenSound.SetActive (false);



 
 }
 

 void Update () { //The main function wich controlls how the system will work.

     if (T_ActivatedOpen == true)
         T_ActivatedClose = false;

     else if (T_ActivatedClose == true)
         T_ActivatedOpen = false;

     if (activateTrigger && Input.GetKeyDown(KeyCode.E)) //For some reaseon you can't have both E (open and close).
     {
             textO.SetActive (false);
             textC.SetActive (false);
             T_ActivatedOpen = false;
             T_ActivatedClose = true;
             textO.SetActive (false);
             textC.SetActive (true);
             doorOpen = true;

             doorOpenSound.SetActive (true);
             doorCloseSound.SetActive (false);

         if (doorOpen) 
         {
             doorOpen = true;
             doorController ("Open");
             
         }
             
         }
         else if(T_ActivatedClose && activateTrigger && Input.GetKeyDown(KeyCode.F)) 
         {
             T_ActivatedOpen = true;
             T_ActivatedClose = false;
             textO.SetActive (true);
             textC.SetActive (false);

             doorCloseSound.SetActive (true);
             doorOpenSound.SetActive (false);
            
         if (doorOpen) 
         {
             doorOpen = false;
             doorController ("Close");
         }
             
         } 
 }


                                                     
 void OnTriggerEnter(Collider col) //If you enter the trigger this will happen.
 {
     if(col.gameObject.tag == "Player")
     {

             activateTrigger = true;
         if((T_ActivatedOpen == true))
         textO.SetActive (true);

         if((T_ActivatedClose == true))
             textC.SetActive (true);
     }
     
 }


 void OnTriggerExit(Collider col) //If you exit the trigger this will happen.
 {
     if(col.gameObject.tag == "Player")
     {
         textO.SetActive (false);
         textC.SetActive (false);
         activateTrigger = false;
     }

 }

 void doorController(string direction) //Animator function.
 {
     animator.SetTrigger(direction);
 }
     

}

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
1

Answer by Spidlee · Mar 22, 2017 at 02:39 AM

I believe you have an error in your logic handling. You shouldn't need to use different keys to open / close a door.

Very roughly this is what I would do:

 bool isDoorOpen = false; 
 public TextMesh keyPressPrompt; 
 string s_actionKey = "Press E"; 
 string s_openDoor = "Open Door"; 
 string s_closeDoor = "Close Door"; 
 
 void OpenCloseDoor ()
 {
     // handle relevant open / close door animations
     if(isDoorOpen)
     {
         // close door animation
     }
     else
     {
         // open door animation
     }
     
     // toggle door open state
     isDoorOpen = !isDoorOpen;     
 }
 
 
 void KeyPressPrompt (bool activate)
 {
     if(!activate)
     {
         // handle deactivation animation etc
     }
     else
     {
         // handle activation animation etc
         
         // show Close Door Prompt if Door is Open, vice versa
         keyPressPrompt.text = (isDoorOpen ? s_closeDoor : s_openDoor) + "\n" + s_actionKey; 
     }
 }
 
 void CheckForKeyInput ()
 {
     if(Input.GetKeyDown(KeyCode.E))
         OpenCloseDoor(); 
 }
 
 void Start ()
 {
     // init stuff
 }
 
 void Update ()
 {
     CheckForKeyInput(); 
 }
 
 void OnTriggerEnter (Collider col)
 {
     if(col.gameObject.tag == "Player")
         KeyPressPrompt(true); 
 }
 
 void OnTriggerExit (Collider col)
 {
     if(col.gameObject.tag == "Player")
         KeyPressPrompt(false); 
 }

Hope that helps get you on the right direction.

Comment
Add comment · Show 2 · 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 psych77 · Mar 23, 2017 at 11:37 AM 0
Share

Thanks alot man! I was able to make it work!

Also one more question if i could bug you further.. Any ideas how to add audio to the scene? Two clips, one for open and one for close? Tried many combinations, but got into trouble when player collides with door. It then triggers sound as well as when i open door.

avatar image Spidlee psych77 · Mar 23, 2017 at 07:49 PM 0
Share

Not really sure I understand your question.

But to handle playing the right audio clip, I would just play the appropriate audio clip in OpenCloseDoor(), using if(isDoorOpen) to deter$$anonymous$$e which to play.

What I usually do for a more indepth solution would be to create an Audio$$anonymous$$anager, when I organise all possible audio clips to be played in the game, and call the relevant functions to play what I need.

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Opening a door while holding a key 1 Answer

How can I control each door lock state individual and also to control all the doors lock states ? 0 Answers

Script for a door that opens and closes 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