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 /
This question was closed Aug 03, 2014 at 08:20 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by The-W.A.T.Z.R · Aug 02, 2014 at 03:16 PM · javascriptdoorkeypress

Door Open on keydown and Close Door on same keydown?

yeah like you see in the Q titel i need to change the script a little.

now the behaviour on the door is F to open and then the door closes by himself so how do i change this so when i open it then it gonna be still open until I press F again to close it.

(easier said, that I do not want that door closes automatiskt, for I have to close the door on the same key-(F)

but atfer then I want to put in so the door closes aoutomatiskt
for we say that when a player opens a door and leave it open and run away, then I want that door to close automatically, we say after 1h. or the player can stay there and wait 1h and the door close, he actually don't need to run away but you get the point :)

Script:

 #pragma strict
 
 var theDoor : Transform;
 private var drawGUI = false;
 private var doorIsClosed = true;
 
 function Update () 
 {
     if (drawGUI == true && Input.GetKeyDown(KeyCode.F))
     {
         changeDoorState();
     }
 }
 
 function OnTriggerEnter (theCollider : Collider)
 {
     if (theCollider.tag == "Player")
     {
         drawGUI = true;
     }
 }
 
 function OnTriggerExit (theCollider : Collider)
 {
     if (theCollider.tag == "Player")
     {
         drawGUI = false;
     }
 }
 
 function OnGUI ()
 {
     if (drawGUI == true)
     {
         GUI.Box (Rect (Screen.width*0.5-51, 200, 102, 22), "Press F to open");
     }
 }
 
 function changeDoorState ()
 {
     if (doorIsClosed == true)
     {
         theDoor.animation.CrossFade("Open");
         //theDoor.audio.PlayOneShot();
         doorIsClosed = false;
         theDoor.animation.CrossFade("Close");
         //theDoor.audio.Play();
         doorIsClosed = true;
     }
 }




ThX in advance

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

  • Sort: 
avatar image
1
Best Answer

Answer by ShadyProductions · Aug 02, 2014 at 03:27 PM

You can try something like this

 //add this in your update function
         if (Input.GetKeyDown(KeyCode.F))
         {
             changeDoorState();
         }
     
     function changeDoorState ()
     {
         if (doorIsClosed)
         {
             theDoor.animation.CrossFade("Open");
             //theDoor.audio.PlayOneShot();
             doorIsClosed = false;
         }
         else
         {
             theDoor.animation.CrossFade("Close");
             //theDoor.audio.Play();
             doorIsClosed = true;
         }
     }
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 The-W.A.T.Z.R · Aug 02, 2014 at 04:44 PM 0
Share

hey thx i didn't need to add

 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F) && (!doorIsClosed)
         {
             changeDoorState();
         }

its work without it i only get errors when i use it.

do you have a solution for the door close automatically after 1h? for i have this solution but i don't get it to really work the door is closed after 5 sec but if i open and close the door then after 5 sec then the door close animation starts play once how to stop that if the door is already closed

script now :

 #pragma strict
  
 var theDoor : Transform;
 private var drawGUI = false;
 private var doorIsClosed = true;
  
 function Update () 
 {
     if (drawGUI == true && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
     {
         changeDoorState();
     }
 }
 
 function OnTriggerEnter (theCollider : Collider)
 {
     if (theCollider.tag == "Player")
     {
         drawGUI = true;
     }
 }
  
 function OnTriggerExit (theCollider : Collider)
 {
     if (theCollider.tag == "Player")
     {
         drawGUI = false;
     }
 }
  
 function OnGUI ()
 {
     if (drawGUI == true)
     {
         GUI.Box (Rect (Screen.width*0.5-51, 200, 102, 22), "Press F to open");
     }
 }
  
 function changeDoorState ()
     {
         if (doorIsClosed)
         {
             theDoor.animation.CrossFade("Open");
             //theDoor.audio.PlayOneShot();
             doorIsClosed = false;
             yield WaitForSeconds(5);
             theDoor.animation.CrossFade("Close");
             //theDoor.audio.Play();
             doorIsClosed = true;
         }
         else
         {
             theDoor.animation.CrossFade("Close");
             //theDoor.audio.Play();
             doorIsClosed = true;
         
         }
 }
avatar image ShadyProductions · Aug 02, 2014 at 05:49 PM 0
Share

You make it way too difficult

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

[Closed]Script that opens doors with the press of a key. 0 Answers

Setting Scroll View Width GUILayout 1 Answer

Mod-ing a Unity Door Key Switch 1 Answer

How Do I make my door always open when I press the key? 2 Answers

How to set-up a Door that locks behind you after walk in to a room, but need a iPhone as a key to get out? 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