Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Timvb_97 · Jul 06, 2020 at 12:37 PM · soundaudiosourceontriggerenterdelayvoice

What to do to hear a voice over only 1 time?

Hi! I have a question. In my game, you hear a voice over. Right now, the voice overs (different audio sources) are connected to the player. Example, the player walks to an empty bottle and grabs it (VR), then you will hear a voice over "Hmm, empty, I need to refill this". This bottle is connected to a script with 2 public audio sources and a void OnTriggerEnter, as follows

  public class TriggerSFX : MonoBehaviour
  {
      public AudioSource playSound;
      public AudioSource playSound2;
  
      void OnTriggerEnter(Collider other)
      {
          playSound.Play();
          playSound2.Play();
      }
  }

The audio source with the line "Hmm, empty, I need to refill this" is connected to the player. This audio source will be put in the public audio source playSound. This works, but not as I want it to do. I only want the sound to be heard just 1 time. Right now, you will hear this sound everytime you pick the bottle up. And further, you see 2 audio sources and 2 triggers. This is because in some cases, I want 2 voice overs on one trigger but the second voice over a bit later, couple seconds, with delay. I've searched the whole internet to find the answers for both questions and tried many things, but I just couldn't figure it out. Can someone help me with this?

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
0
Best Answer

Answer by Captain_Pineapple · Jul 06, 2020 at 01:14 PM

okay here we go.

this script should fix most of your problems. It can play dialogue delayed or one after the other. This version however will specifically play each audio once per object. So if there are 2 bottles both will play the audio once. Hope this is what you wanted, let me know if something is unclear here.

 public class TriggerSFX : MonoBehaviour
     {
         public AudioSource playSound;
         public AudioSource playSound2;
         //boolean flag to keep track if the OnTriggerEnter function of *this specific* object has been triggered yet.
         private bool hasPlayed = false;
 
         void OnTriggerEnter(Collider other)
         {
             if (hasPlayed)
                 return;
             hasPlayed = true;
             playSound.Play();
             //this will play the sound 2 after a time of 10 seconds. The Gameobject/Component must not be disabled in this time.
             StartCoroutine(delayedSFX(playSound2, 10f));
             //alternative approach to play sound if the first one has finished:
             StartCoroutine(SFXEnqueue(playSound, playSound2));
         }
 
         private IEnumerator delayedSFX(AudioSource playSource, float timeDelay)
         {
             yield return new WaitForSeconds(timeDelay);
             if(!playSource.isPlaying)
                 playSource.Play();
         }
 
         private IEnumerator SFXEnqueue(AudioSource waitForSource, AudioSource playSource)
         {
             yield return null;
             while(waitForSource.isPlaying)
             {
                 yield return null;
             }
             if (!playSource.isPlaying)
                 playSource.Play();
         }
     }
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 Timvb_97 · Jul 08, 2020 at 10:14 AM

Hi @Captain_Pineapple, thank you so so much! It works now! Exactly how I want it to, yesss!

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

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

Related Questions

recording sound from external input 0 Answers

[NoBraves] Sound makes lag 1 Answer

GetSpectrumData doesn't update 2 Answers

need help to executing sound 3 Answers

3D Audio: Selectively Apply the Doppler Effect. 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