Playing A Sound at a Location From Code

Currently I have this working by creating an object that, when created, plays a sound, so I just create that object where I want the sound to be played from.

My question is: Is there a simpler way to play a sound at a point without me having to create a lot of different objects?

Sure, the below code will create an audiosource and once the clip finishes playing, it will automatically delete it.

var clip : AudioClip;

function Update(){
    if(condition){
        AudioSource.PlayClipAtPoint(clip, Vector3 (5, 1, 2));
    }    
}
1 Like

No, a positional sound requires that you have an object at that position.

but you can avoid the create and destroy through having a pool (:Array) where you push and pop game objects with attached audiosources and when you find out that it is empty, you create a new one (to drop in on the sound end)

saves you the create and destroy which especially on the iphone can be pretty beneficial

when i put the code in how do I use it?

Please don’t necro-post to 12-year-old threads. Start your own post… it’s FREE.

If you have no idea where to put a script, posting here will not help you.

You probably haven’t spent enough time with the basics.

This is Unity’s main gateway to learning how to work with Unity. Click here and get busy!

If you have a technical question, use these steps to report your issue:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

1 Like

This was written in UnityScript and not C#. This language does no longer exist and is no longer supported. However the actual method does still exists, namely “AudioSource.PlayClipAtPoint”. He even linked to the documentation which does have a C# example. This question was about creating an audio source from code. So you have to know where or when to call that method. We can’t tell you when you may want to call it.

I’ve been using AudioSource.PlayClipAtPoint for a while thinking it was helpful and… well, it’s a performance drain if you use it a lot, such as a machine gun.

Looking at the source code:

public static void PlayClipAtPoint(AudioClip clip, Vector3 position, [UnityEngine.Internal.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(gameObject, clip.length * ((Time.timeScale < 0.01f) ? 0.01f : Time.timeScale));
        }

As you can see, this is a not a good idea. There is no way to not use the static method, it would be awesome if we did have the audioSource.play(position) in there, but it needs to have a created game object at the moment.

Well, the documentation was quite clear about that:

Not sure what you mean by that. This method exists just for convenience. Of course when you need to play a lot of audio, you probably want to cache and reuse your audio sources and probably limit the number of sources. This could be done on a per-gun basis (so every gun has a certain amount of audio sources) or globally. Though global pools for things like that, especially when they are limited can easily create issues like the silent BFG (if you prefer watching a video of john romero explaining it, here you go. I can recommend the whole talk if you can spend the hour) :wink:

So when using pools you really should think about how many you really need and how to manage them.

1 Like