Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by LeftyTwoGuns · Nov 23, 2015 at 11:00 PM · soundaudiosourceloopaudioclip

Playing looped sound while holding button is distorted

I'm trying to get a looped sound effect to play while holding down a button to charge a projectile like Metroid/Mega Man. I have two different types of projectiles that can be charged, so two different looped clips. Both clips loop fine and sound great when I play them looped in the inspector but when they play in game, they're both very loud and distorted. Here's the script:

     private AudioSource sound;
     public AudioClip blueCharge;
     public AudioClip redCharge;
 
 void Awake()
     {
         sound = GetComponent<AudioSource>();
     }
 
 void Update()
     {
         if (blueCharging)
         {
             sound.PlayOneShot(blueCharge);
         }
         
         else
         if (redCharging)
         {
             sound.PlayOneShot(redCharge);
 
        }
 }

The two bools are just bools from another script that tell me what weapon is selected and the button is being held down. The Audio Source is hooked up to a Audio Mixer Output, which I haven't messed with at all, and no matter if I set Loop to true or false in the inspector, it makes no difference. What could I be doing wrong?

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 m0guz · Nov 24, 2015 at 12:20 AM 1
Share

$$anonymous$$ost likely playing audio clip goes into loop. In this case, quick fix can be checking if sound/AudioSource is playing any clip. If not, play sound.

if (blueCharging && !sound.IsPlaying)

2 Replies

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

Answer by MCoburn · Nov 24, 2015 at 01:00 AM

You're possibly experiencing audio overlap, which is two sounds at the same position in space playing at the same time. Since the audio both overlap, the volume becomes louder, and can lead to distortion.

Adding what Avietry suggested will make it only play should the sound not be playing. If it's playing, then it'll do nothing and carry on. Else, it'll play the sound.

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 LeftyTwoGuns · Nov 24, 2015 at 02:47 AM 0
Share

That was it, though I had to do it like

 if(blueCharging)
 {
     if(!sound.isPlaying)
            blah blah blah
 }

For it to work. Thank you both very much!

avatar image
0

Answer by Mich_9 · Nov 24, 2015 at 03:23 PM

That's because you are creating instances of the same sound effect again and again every frame update, for that is PlayOneShot.

 private AudioSource sound;
 public AudioClip blueCharge;
 public AudioClip redCharge;
 
     void Awake()
     {
         sound = GetComponent<AudioSource>();
     }
 
     void Update()
     {
         bool ischarging = false;
         //Set audioclip to play
         if (blueCharging)
         {
             sound.clip = blueCharge;
             ischarging = true;
         } else if (redCharging)
         {
             sound.clip = redCharge;
             ischarging = true;
         } else
         {//Optional
             sound.Stop();//Stop playing the audio if not charging
         }
         //Check if the sound is not already playing and if is charging
         if (!sound.isPlaying && ischarging)
         {
             sound.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

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

33 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

Related Questions

Need help: new sound isnt played correctly anymore 2 Answers

Second AudioClip won't play 0 Answers

Audio is way too soft on mobile but alright in Unity Editor! 0 Answers

Play sound within limited region 2 Answers

Audio cut off in new WebGL builds 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