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
12
Question by SergeantBiscuits · Sep 11, 2012 at 11:40 PM · audiosourceplayclipatpoint

Adjust properties of AudioSource created with PlayClipAtPoint

What up UA, quick question. Recently discovered "PlayClipAtPoint", which is super handy. However I want to set the volume/pitch/etc. of the AudioSource that is created. Is there a simple way to do this?

(I don't fully understand exactly what criterion PlayClipAtPoint uses to generate the temporary AudioSource.)

Thanks so much!

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

Answer by aldonaletto · Sep 12, 2012 at 12:36 AM

PlayClipAtPoint creates a temporary game object called "One shot audio" that contains the AudioSource, and destroy it when the sound finishes. Unfortunately, it doesn't give us any reference to the object created - shame on you, Unity: PlayClipAtPoint should return a reference to the AudioSource, or at least to the object created!

An alternative could be to find the temporary object with GameObject.Find("One shot audio"), but this would fail if more than one "One shot audio" existed. But there's a solution: create your own PlayClipAtPoint function, like this:

 function PlayClipAt(clip: AudioClip, pos: Vector3): AudioSource; {
   var tempGO = GameObject("TempAudio"); // create the temp object
   tempGO.transform.position = pos; // set its position
   var aSource = tempGO.AddComponent(AudioSource); // add an audio source
   aSource.clip = clip; // define the clip
   // set other aSource properties here, if desired
   aSource.Play(); // start the sound
   Destroy(tempGO, clip.length); // destroy object after clip duration
   return aSource; // return the AudioSource reference
 }

You can set other AudioSource properties inside PlayClipAt, or use the returned AudioSource reference to access these properties in the calling code, like this:

   ...
   var sound = PlayClipAt(someClip, transform.position);
   sound.pitch = 1.5;
   sound.volume = 0.5;
   ...
Comment
Add comment · Show 9 · 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 SergeantBiscuits · Sep 12, 2012 at 01:21 AM 0
Share

It was as I feared! I was under the impression I would need to write my own function. The framework you cooked up there looks great; I will roll with that!

Thank you so much for taking the time to help me out! :] Fellas like you make this community awesome!

avatar image metaphysician · Mar 03, 2013 at 10:01 PM 0
Share

hi Aldo - do you or anyone else have any clue how to represent that above procedure in C#? i figured out parts of it but structurally C# is going to want a type declaration after void. but typing 'void AudioSource PlayClipAt' doesn't seem to work. any clues?

avatar image aldonaletto · Mar 04, 2013 at 12:38 PM 4
Share

The C# version is something like this:

 AudioSource PlayClipAt(AudioClip clip, Vector3 pos){
   GameObject tempGO = new GameObject("TempAudio"); // create the temp object
   tempGO.transform.position = pos; // set its position
   AudioSource aSource = tempGO.AddComponent<AudioSource>(); // add an audio source
   aSource.clip = clip; // define the clip
   // set other aSource properties here, if desired
   aSource.Play(); // start the sound
   Destroy(tempGO, clip.length); // destroy object after clip duration
   return aSource; // return the AudioSource reference
 }

NOTE: This version was not compiled, thus may contain some stupid C# errors.

avatar image lucassivolella aldonaletto · Sep 23, 2020 at 01:12 PM 0
Share

Thanks a lot for this! Hope you did well all this years.

avatar image metaphysician · Mar 05, 2013 at 09:12 AM 0
Share

thanks for this, Aldo! much appreciated...

avatar image kalamona · Jul 01, 2013 at 03:00 PM 0
Share

This is a great solution, thank you very much!

Show more comments
avatar image
5

Answer by BowlerBitesLane · Aug 28, 2015 at 11:15 AM

Here's a quick and dirty adaptation to allow the passing of AudioSource Objects into the mix so that you can copy the properties. This may be helpful in certain situations where you want to be able to assign an AudioMixer as well as adjust other properties within the inspector:

 // copies audiosource properties to temp audiosource for playing at a position
     public static AudioSource PlayClipAtPoint(AudioSource audioSource, Vector3 pos)
     {
         GameObject tempGO = new GameObject("TempAudio"); // create the temp object
         tempGO.transform.position = pos; // set its position
         AudioSource tempASource = tempGO.AddComponent<AudioSource>(); // add an audio source
         tempASource.clip = audioSource.clip;
         tempASource.outputAudioMixerGroup = audioSource.outputAudioMixerGroup;
         tempASource.mute = audioSource.mute;
         tempASource.bypassEffects = audioSource.bypassEffects;
         tempASource.bypassListenerEffects = audioSource.bypassListenerEffects;
         tempASource.bypassReverbZones = audioSource.bypassReverbZones;
         tempASource.playOnAwake = audioSource.playOnAwake;
         tempASource.loop = audioSource.loop;
         tempASource.priority = audioSource.priority;
         tempASource.volume = audioSource.volume;
         tempASource.pitch = audioSource.pitch;
         tempASource.panStereo = audioSource.panStereo;
         tempASource.spatialBlend = audioSource.spatialBlend;
         tempASource.reverbZoneMix = audioSource.reverbZoneMix;
         tempASource.dopplerLevel = audioSource.dopplerLevel;
         tempASource.rolloffMode = audioSource.rolloffMode;
         tempASource.minDistance = audioSource.minDistance;
         tempASource.spread = audioSource.spread;
         tempASource.maxDistance = audioSource.maxDistance;
         // set other aSource properties here, if desired
         tempASource.Play(); // start the sound
         MonoBehaviour.Destroy(tempGO, tempASource.clip.length); // destroy object after clip duration (this will not account for whether it is set to loop)
         return tempASource; // return the AudioSource reference
     }
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

17 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

Related Questions

Making AudioSource follow camera? 1 Answer

AudioSource.PlayClipAtPoint 1 Answer

Why am I able to play an AudioClip without an AudioSource? 1 Answer

How do I properly extend AudioSource.PlayClipAtPoint? 1 Answer

A better script for a machine gun? 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