- Home /
How to route sound from PlayClipAtPoint() to a specific audio mixer group?
The only other answer I've found to a similar problem is to add the mixer as a public variable. But this can't be done when using PlayClipAtPoint().
I've tried to write my own public static method that does what PlayClipAtPoint does, but I can't change the audio mixer group in the script after adding the audiosource component.
Any ideas on how to achieve this? Thanks!
Answer by neekoh · Jun 23, 2016 at 02:13 PM
Hi,
I don't know why you can't change the outputAudioMixerGroup after adding the audio source. Also, PlayClipAtPoint() also uses default values for all AudioSource parameters for the temporary objects it makes, so if you want some specific behaviour, PlayClipAtPoint() is not very useful.
But with your replacement method you can do this:
Create a set of prefabs of gameobjects that have an AudioSource component
In the prefabs you can set different mixer groups and other AudioSource settings
In you replacement method instantiate one of these prefabs as your temporary audio source depending on situation. Something like:
GameObject audioObject; if (type == AudioType.Announcer) audioObject = Instantiate(announcerAudioPrefab); else audioObject = Instantiate(worldAudioPrefab); audioObject.transform.position = position; AudioSource tAudio = audioObject.GetComponent<AudioSource>(); tAudio.clip = clip; tAudio.Play();
Answer by FColor04 · Dec 30, 2021 at 07:23 PM
It's old question but for anyone still wondering, static method
PlayClipAtPoint
looks like this:
public static void PlayClipAtPoint
(AudioClip clip, Vector3 position, [DefaultValue("1.0F")] float volume)
{
GameObject gameObject = new GameObject("One shot audio");
gameObject.transform.position = position;
AudioSource audioSource = (AudioSource) gameObject.AddComponent(typeof (AudioSource));
audioSource.clip = clip;
audioSource.spatialBlend = 1f;
audioSource.volume = volume;
audioSource.Play();
Object.Destroy((Object) gameObject, clip.length *
((double) Time.timeScale < 0.009999999776482582 ? 0.01f : Time.timeScale));
}
And after some modifications I've got this in result:
public static void PlayClipAtPoint
(AudioClip clip, Vector3 position, float volume = 1.0f, AudioMixerGroup group = null)
{
if (clip == null) return;
GameObject gameObject = new GameObject("One shot audio");
gameObject.transform.position = position;
AudioSource audioSource = (AudioSource) gameObject.AddComponent(typeof (AudioSource));
if(group != null)
audioSource.outputAudioMixerGroup = group;
audioSource.clip = clip;
audioSource.spatialBlend = 1f;
audioSource.volume = volume;
audioSource.Play();
Object.Destroy(gameObject, clip.length *
(Time.timeScale < 0.009999999776482582 ? 0.01f : Time.timeScale));
}
Your answer
Follow this Question
Related Questions
Is it possible to expose the output of a Unity Audio Mix Bus as an AudioSource? 0 Answers
GoogleVr Audio Audiosource - Output 1 Answer
Get spectrum data from specific Audio Mixer? 1 Answer
Is there a way to limit the output volume on an audio mixer group? 1 Answer
Making AudioSource follow camera? 1 Answer