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!
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;
...
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
}