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 Sickleadz · Jan 25, 2014 at 10:17 PM · soundplaydooropenonce

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

Hey Everybody

in my scene i have a door that you can open by pressing F then it also plays a sound from the doors Audio Source, but i need the audio to only play one time, since the door cannot be closed again,

So when the door is open and i press F at the open door it stills plays the audio

Can you help me fix script so it doesnt ?

THanks in advance

Here's my script

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

if(Input.GetKeyDown("f") && enter){ open = !open; audio.Play(); } }

//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 · 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 oatsbarley · Jan 25, 2014 at 11:02 PM 0
Share

Please format your code by highlighting it and clicking the little button with the 1s and 0s in the editor.

3 Replies

· Add your reply
  • Sort: 
avatar image
-2

Answer by Sickleadz · Jan 25, 2014 at 11:53 PM

Thanks for the fast answer

i changed the code to what your wrote but the sound still plays, when door is open and i press f

hmmm, weird..

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 superluigi · Jan 26, 2014 at 12:35 AM 1
Share

When you respond to someone use comments. Also format your code so people can read it. Watch the unity answers tutorial video. It's always at the right of the page.

avatar image oatsbarley · Jan 26, 2014 at 09:32 AM 0
Share

Okay, I've edited my answer slightly. You had it so that open toggled between true and false when you pressed F, now it only changes to true.

avatar image
0

Answer by oatsbarley · Jan 25, 2014 at 11:05 PM

Where you have audio.Play() you need to check whether the door is open already:

 if (Input.GetKeyDown("f") && enter) {
     if (!open) audio.Play();
     open = true;
 }

That should only play the audio when the door is closed.

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
avatar image
0

Answer by superluigi · Jan 26, 2014 at 01:02 AM

The main problem is here

 function Update ()
 { 
     if(open)
     { 
         transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth); 
     }
 
     if(Input.GetKeyDown("f") && enter && !open)
     { 
         open = true; 
         audio.Play(); 
     }
  }

By adding open to the if condition now you can only do it once. When you press f and enter and the doors open = false then play the audio and open becomes true. So now that open = true it no longer meets the if statements conditions (input F key(yes), enter(yes), !open(no)) get it?

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 superluigi · Jan 26, 2014 at 01:04 AM 0
Share

If you need help understanding my answer let me know and I will explain it. As it is though it should work.

avatar image oatsbarley · Jan 26, 2014 at 09:35 AM 0
Share

As far as I can make out, open being true does mean the door is open in @sickleadz code. Although it's not exactly clear and I could be misreading it!

avatar image superluigi · Jan 26, 2014 at 10:40 PM 0
Share

Yea it confused me at first. I was gonna change it for him but I left it alone. I think I'm gonna rewrite it for him and just have him copy and paste.

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

20 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

Related Questions

Make door play sound when you open and close it ? 1 Answer

Audio issue - Trying Waitforseconds 0 Answers

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

Play sound ONCE! 3 Answers

Can not play a disabled audio source persistent problem 0 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