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
2
Question by Earless · Oct 13, 2014 at 02:53 PM · audioaudiosourceaudioclip

No overlapping sounds

Hey guys,

quick question (I hope). How do I stop audio clips from overlapping? I'm creating a Pacman game, and when Pacman eats a candy, I want a sound to play. However, if an eating sound is still playing, I dont want the new sound to start. Only if one eating sounds is completed, and Pacman eats another candy, I want the clip to play again. Partial piece of my code for this:

 public AudioClip candy;
 
 void OnTriggerEnter(Collider other) 
     {
         if (!pause)
         {
             if (other.gameObject.tag == "PickUp")
             {
                 other.gameObject.SetActive(false);
                 if (!audio.isPlaying)
                     AudioSource.PlayClipAtPoint(candy, transform.position);
                 count = count + 1;
                 score = score + 10;
                 //scoreText.text = "Score: " + score;
                 if (count >= totalPickupCount)
                 {
                     if (!fail)
                     {
                         timeScore = Mathf.FloorToInt(10 * startTimer);
                         liveScore = Lives.lives * 100;
                     }
                     storeEndScore();
                     storeEndTimeScore();
                     storeEndLiveScore();
                     Application.LoadLevel("Score"); //Game is klaar - ga naar Score Scherm
             
                 }
             }
       }

As you can see, i treid using audio.isplaying. However this has no effect. Any other ideas/options?

Comment
Add comment · Show 10
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 SaraCecilia · Oct 13, 2014 at 01:51 PM 0
Share

Hi, please check your code once more. Like adding curly brackets around: AudioSource.PlayClipAtPoint(candy, transform.position); count = count + 1; score = score + 10;

avatar image Earless · Oct 13, 2014 at 09:50 PM 0
Share

Is not needed, since count = count + 1 shouldnt be in the if statement :)

avatar image KpjComp · Oct 13, 2014 at 10:29 PM 0
Share

audio, and AudioSource.PlayClipAtPoint I believe don't use the same AudioSource, and I think isPlaying is a flag for if you have it paused anyway.. But if you use your own AudioSource ins$$anonymous$$d, you can check what's playing by checking it's output data and see if it's playing anything. Here is a function that does it.

     bool isPlaying(AudioSource p) {
         float[] samples = new float[1024];
         audio.GetOutputData(samples, 0);
         for(int l = 0; l < 1024; l ++) {
             if (samples[l] != 0) return true;
         }
         return false;
     }

Alternatively you could also set a flag to say if your playing audio, to unset the flag check the length of the audio, and with a timer unset after that length.

avatar image MrSoad · Oct 13, 2014 at 10:41 PM 0
Share

I think $$anonymous$$pjComp is on the right track here. The :

 if (!audio.isPlaying) {

Will only check if the Audio Source that is on the same object that the script is on is Playing. If you want to check for a playing audio source on another object you need to reference that object like :

 if (!$$anonymous$$y_Other_GameObject.audio.isPlaying) {

I can't comment on the play clip at point, never used it sorry.

avatar image KpjComp · Oct 13, 2014 at 10:52 PM 0
Share

$$anonymous$$rSoad you a right here, the docs for isPlaying I would say is wrong though. >> AudioSource.isPlaying will return false when AudioSource.Pause() is called.. isPlaying does indeed return false when audio has stopped playing too, not just when pause is called. So my isPlaying function is not needed. Just using an AudioSource ins$$anonymous$$d of PlayClipAtPoint does the trick.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio loops too early 2 Answers

detect audio then record and then play 0 Answers

Audio clip not playing 1 Answer

WWW Audio and PlayOneShot: Playing the same AudioClip Twice Cuts off First Instance 1 Answer

AudioClip.Create() + PlayClipAtPoint() Not Working 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