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 Nov 13, 2013 at 04:21 PM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by kenny0630 · Nov 13, 2013 at 01:14 AM · javascriptprojectdoorsound effects

How to add a sound effect on a Door?

I am pretty new to Unity and I am doing a project on making a character to move around. I have successfully move the character around by using Character Controller and manage to open and close the door by pressing the F and G keys respectively.

The problem now I faced is that I can't seem to put a open and close door sound effect to the door... I have the following javascripts:

 var smooth = 2.0;
 var DoorOpenAngle = 90.0;
 var DoorCloseAngle = 0.0;
 var open : boolean;
 var enter : boolean;
 var DoorOpen : AudioClip;
 var DoorClose : AudioClip;
 
 //Main 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.GetKeyDown("f"))
         {
             (open) = true;
         }
         else if(Input.GetKeyDown("g"))
         {
             (open) = false;
         }
     }
 }
 
 //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 away from the door
 function OnTriggerExit (other : Collider)
 {
     if(other.gameObject.tag == "Player")
     {
         (enter) = false;
     }
 }
 
 var Trigger : AudioClip;
 //When player enters the trigger zone
 function OnTriggerStay()
 {
     if(Input.GetKeyDown("f"))
     {
         if (open) audio.PlayOneShot(DoorOpen);
         else audio.PlayOneShot(DoorClose);
     }
 }

The sound is just not playing or anything...Please do help me.. Thank you very much!!

Comment
Add comment · Show 3
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 AndyMartin458 · Nov 13, 2013 at 02:04 AM 0
Share

Try putting a debug log inside of the on trigger stay function to make sure that those calls to audio are being made.

avatar image thenachotech1113 · Nov 13, 2013 at 02:16 AM 0
Share

well, i have not used javascript in a while, but there could be many things going on, including a bug. so just try to use debug.log() to find out where the problem is

avatar image kenny0630 · Nov 13, 2013 at 04:02 PM 0
Share

Andy$$anonymous$$artin458 & thenachotech1113: I tried to put the debug.log(); into the OnTriggerStay function but it says unknown identifier..... or is there any other way to add the sound?

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by kenny0630 · Nov 13, 2013 at 04:18 PM

Problem solved.... it happened to be a misplace of the Audio Source... It should be placed under the parent not the child Door... and I've changed the scripts of the OnTriggerStay function slightly:

 function OnTriggerStay()
 {
     if(Input.GetKeyDown("f"))
     {
        audio.PlayOneShot(DoorOpen);
     }
     if(Input.GetKeyDown("g"))
     {
        audio.PlayOneShot(DoorClose);
     }
 }

and now it works perfectly fine with opening and closing the door~ :) Thank you for the help guys~

Comment
Add comment · Show 1 · 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 fafase · Nov 13, 2013 at 04:21 PM 0
Share

Nice you made it, still consider what I am saying in my answer. It works, but it may be that it will work most of the time but not all the time.

avatar image
1

Answer by fafase · Nov 13, 2013 at 04:20 PM

It is not a good idea to mix Input and Physics.

OnTriggerStay is done by the physics engine while Input comes fro your OS.

Both are not running the same speed. GetButtonDown returns true only on the frame you are pressing, if the physics is not run on that frame, nothing happens.

I would suggest to modify your code, so that the Input is taken care in the Update and the Trigger is using Enter and Exit to set a boolean on and off.

 var isIn:boolean = false;
 function OnTriggerEnter()
 {
     isIn = true;
 }
 function OnTriggerExit()
 {
     isIn = false;
 }
 
 function Update()
 {
     if(Input.GetKeyDown("f") && open && isIn){
         audio.PlayOneShot(DoorOpen);
     }
 }
Comment
Add comment · Show 1 · 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 kenny0630 · Nov 19, 2013 at 02:34 PM 0
Share

thank you for your help! I have manage to swap the codes!! if(enter == true) { if(Input.Get$$anonymous$$eyDown("f")) { (open) = true; audio.PlayOneShot(DoorOpen); } else if(Input.Get$$anonymous$$eyDown("g")) { (open) = false; audio.PlayOneShot(DoorClose); } }

I've placed it in the update function.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Sound effect on right click 1 Answer

A question on distance 1 Answer

What scripting language should I use for a multiplayer project? 1 Answer

How would I call upon an asset in the projects tab with an array? 1 Answer

Door Opening And Closing 2 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