- Home /
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!
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;
...
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!
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?
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.
Thanks a lot for this! Hope you did well all this years.
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
}