• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by greengiant1177 · Jan 14, 2013 at 02:33 AM · sounddoor

Openable door by keypress with two different sounds

Hi

I have been searching for this a lot, didnt find a complete solution that I was able to get to work, started to puzzle scripts together wich was my first success of coding =) =P. My door opens and closes, it's a sound source with audio file attached, 2scripts on it; 1; door opens and closes on e-key press, 2; triggers sound source also on e key. But, i want 2 different sounds, one 4 open, one 4 closing. all on e-key press. So, there is Parent;emptygame obeject wich is trigger and has the two scripts, and there is the door mesh as child.
Here's the scripts: door open and close:

 //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;
     
 //Main 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);
         }
     
         if(enter == true)
     {
             if(Input.GetKeyDown("e"))
         {
                 open = !open;
             }
         }
 }
     
 //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
     
     
 //sound from door script:
     
 var Trigger : AudioClip;
 
 //when he enters the trigger zone 
 function OnTriggerStay()
 { 
     //if he presses the e key
     if (Input.GetKeyDown ("e")) 
     {
         //audio plays
         audio.Play();
     }
 }

Any1 know a solution ? perhaps it's neater with one script that does it all.

Thanks/ Joakim

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 Lovrenc · Jan 14, 2013 at 02:42 AM 1
Share

Use the button that says

 101
 010

to format the code.

2 Replies

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

Answer by Navij · Jan 14, 2013 at 11:15 AM

If i understood correctly, you need to add two public audioclip fields to your script:

 var audio1 : AudioClip;
 var audio2 : AudioClip;

and change this line

 audio.Play(); //audio plays

to this:

 if (open) audio.PlayOneShot(audio1);
 else audio.PlayOneShot(audio2);
Comment
Add comment · Show 3 · 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 greengiant1177 · Jan 16, 2013 at 07:40 AM 0
Share

Thanks, But I can't get anything to work, I tried much. I think I still lack the basics of coding =P.

Navij, did u mean what u wrote fixing the "door open" script or for the sound script? I tried both, compiler errors or both sounds but at playstart only and then silence.

Navijs codefix + dooropenscript = comp.error . But here's the try: it complains about missing: open*

//Instruction: //$$anonymous$$ake an empty game object and call it "Door" //Rename your 3D door model to "Body" //Parent a "Body" object to "Door" //$$anonymous$$ake 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 //$$anonymous$$ake 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;

//$$anonymous$$ain 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); }

if(enter == true){ if(Input.Get$$anonymous$$eyDown("e")){ open = !open; } } }

//Activate the $$anonymous$$ain function when player is near the door function OnTriggerEnter (other : Collider){

if (other.gameObject.tag == "Player") { (enter) = true; } }

//Deactivate the $$anonymous$$ain function when player is go away from door function OnTriggerExit (other : Collider){

if (other.gameObject.tag == "Player") { (enter) = false; } }

//sound from door script:

var Trigger : AudioClip; var audio1 : AudioClip; var audio2 : AudioClip;

//when he enters the trigger zone function OnTriggerStay() { //if he presses the e key if (Input.Get$$anonymous$$eyDown ("e")) { if (open) audio.PlayOneShot(audio1); else audio.PlayOneShot(audio2); } }

//@youtube.com/user/maksimum654321

avatar image LPGaming · Jan 16, 2013 at 08:43 AM 0
Share

Stole my comment, xD.... Thumbs up for helping him.

avatar image Rickenbaker · Apr 25, 2013 at 08:40 PM 0
Share

Thank You, this helped me a lot!

avatar image
0

Answer by robertbu · Jan 14, 2013 at 06:23 AM

An easy way would be to just change the clip being played. The open script can set it to the open sound, the close script can set it to the close sound. The reference for AudioSource.clip has a bit of example source that swaps an audio clip.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

DragRigidBody Dynamic Sound Door 1 Answer

Sound play with the speed of the rotation speed of an object. 1 Answer

Sound on doors 1 Answer

Open Door Once But Sound Plays again when i trigger at the door 3 Answers

Make door play sound when you open and close it ? 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