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 jococo · Sep 09, 2013 at 03:07 AM · playclipatpoint

Checking if audio PlayClipatPoint already playing?

Is there a way to check if an audio using PlayClipatPoint is already playing and if not play that clip?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by aldonaletto · Sep 09, 2013 at 04:41 AM

Unfortunately, PlayClipAtPoint doesn't return a reference to the temporary AudioSource it creates, thus we can't check whether it's playing. A simple (although ugly) solution is to GameObject.Find an object called "One shot audio" (that's the name PlayClipAtPoint gives to the temporary game object it creates) - if not found, the clip has finished:

 if (!GameObject.Find("One shot audio")){ // if no temporary audio source...
   AudioSource.PlayClipAtPoint(....); // play the sound
 }

Besides being inelegant, the sound won't be played if there are other temporary AudioSources alive.

If you want to play the sound continuously, a better solution is to repeat the sound at the suitable rate:

 var sound: AudioClip;

 private var tSoundEnd: float;

 function Update(){
   if (Time.time > tSoundEnd){ // if previous clip has finished playing...
     AudioSource.PlayClipAtPoint(sound, ...); // play a new one...
     tSoundEnd = Time.time + sound.length; // and calculate time for next
   }
 }

Finally, you could create your own PlayClipAtPoint function: it could return a reference to the game object created, and when it became null you would know that the clip wasn't playing anymore:

 public var sound: AudioClip;
 
 private var curTempAudio: GameObject;
 
 function PlayClipAt(clip: AudioClip, pos: Vector3): GameObject; {
   var tempGO = GameObject("TempAudio"); // create the temp object
   tempGO.transform.position = pos; // set its position
   tempGO.AddComponent(AudioSource); // add an audio source
   tempGO.audio.clip = clip; // define the clip
   tempGO.audio.Play(); // start the sound
   Destroy(tempGO, clip.length); // destroy object after clip duration
   return tempGO; // return reference to the temporary GameObject
 }
 
 function Update(){
   if (!curTempAudio) curTempAudio = PlayClipAtPoint(sound, transform.position);
 }
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 jococo · Sep 09, 2013 at 07:22 AM 0
Share

Thanks aldonaletto! Funny I already use this awesome technique in another script and forgot.

I should probably also point out that I have a huge swarm of enemies that surround main character and each had a script to play a sound. I ran into serious issues when all 30 starting playing their same sound. So now I'm trying to find a way that if one of the enemies plays the sound the others don't.

I think I can get this to work.

avatar image dimitros89 · Mar 14, 2015 at 12:31 PM 0
Share

checking "One shot audio" works fine!

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

16 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

Related Questions

AudioSource.PlayClipAtPoint 1 Answer

Audio Clip Getting Cut Short 1 Answer

A better script for a machine gun? 1 Answer

Why won't 3d sounds play anywhere near 100% volume? 1 Answer

Making AudioSource follow camera? 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