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 poff · May 13, 2014 at 05:58 PM · inputsounddooropenclose

Door opens, plays sound, but can't close it.

Hey guys, I have a problem with my door. I found this script on youtube, and searched around how to make it play sound. I got it to where when I press E on it, the door opens and plays sound, but for some reason I can't close the door when I open it. Here's the script: (not mine btw)

 // Smothly open a door
 var smooth = 2.0;
 var DoorOpenAngle = 90.0;
 private var open : boolean;
 private var enter : boolean;
 
 private var defaultRot : Vector3;
 private var openRot : Vector3;
 
 function Start(){
 defaultRot = transform.eulerAngles;
 openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
 }
 
 //Main function
 function Update (){
 if(open){
 //Open door
 transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
 }else{
 //Close door
 transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
 }
 
 if (Input.GetKeyDown("e") && enter) {
     if (!open) audio.Play();
     open = true;
 }
 }
 
 function OnGUI(){
 GUI.color = Color.grey;
 if(enter){
 GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press E to Interact");
 }
 }
 
 //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;
 }
 }
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

2 Replies

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

Answer by ThePunisher · May 13, 2014 at 06:06 PM

From what I can see in this script you currently have no way of closing the door. Try replacing the "e" key check with the following:

 if (Input.GetKeyDown("e") && enter)
             {
                 if (!open) audio.Play();
                 open = !open;
             }
Comment
Add comment · Show 5 · 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 ThePunisher · May 13, 2014 at 06:07 PM 0
Share

Basically, this will cause the open flag to alternate every time the player hits the "e" key.

avatar image poff · May 13, 2014 at 06:09 PM 0
Share

Thanks for explaining. I added audio.Play after open line, now the sound plays corectly for opening and closing. Can't tell how happy I am, thanks again.

P.S: Is it possible to add delay for opening and closing ? For example I open the door, it waits for like 1 or 2 seconds and then I can close the door again.

avatar image ThePunisher · May 13, 2014 at 06:34 PM 0
Share

Are you trying to simply add a delay? Or are you trying to wait on the door to finish opening/closing before you allow the player to open/close the door?

avatar image poff · May 13, 2014 at 07:01 PM 0
Share

Yeah, I want so that when I open the door, I can't close it before it finishes opening.

avatar image mrlchristie · May 13, 2014 at 07:49 PM 0
Share

After the bit of code ThePunisher gave you I would then add another if statement to check to see how long the door has been open. For example you could set an "openTime" variable to 2 or 3 seconds (while your open animation plays) in the inspector and then use an if statement to check if those 2 or 3 seconds have passed by using something like

 if(doorOpenTime > openTime){
 ShutDoor();
 doorOpenTime = 0.0f;
 }

to then call a shut door function which will reverse your open animation maybe?

the doorOpenTime to check how long the door has been open can be deter$$anonymous$$ed by using:

doorOpenTime += Time.deltaTime;

and place it in the if statement written by ThePunisher above.

Hope this makes sense, and more importantly, I hope it would work!

avatar image
0

Answer by mrlchristie · May 13, 2014 at 08:13 PM

After the bit of code ThePunisher gave you I would then add another if statement to check to see how long the door has been open. For example you could set an "openTime" variable to 2 or 3 seconds (while your open animation plays) in the inspector and then use an if statement to check if those 2 or 3 seconds have passed by using something like

 if(doorOpenTime > openTime){
 ShutDoor();
 doorOpenTime = 0.0f;
 }

to then call a shut door function which will reverse your open animation maybe?

the doorOpenTime to check how long the door has been open can be determined by using:

 doorOpenTime += Time.deltaTime;

and place it in the if statement written by ThePunisher above.

Hope this makes sense, and more importantly, I hope it would work!

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

22 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

Related Questions

open and close a door with a keypress 6 Answers

Open & Close a simple door animation with sound and door states 1 Answer

open and close door 0 Answers

Open/Close Door Animation disables collider 1 Answer

Need help with door opening and closing script/animation 3 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