AudioSource.PlayClipAtPoint Manual     Reference     Scripting  
Scripting > Runtime Classes > AudioSource
AudioSource.PlayClipAtPoint

static function PlayClipAtPoint (clip : AudioClip, position : Vector3, volume : float = 1.0F) : void

Description

Plays the clip at position. Automatically cleans up the audio source after it has finished playing.

The audio source that is playing the sound is returned

JavaScripts
// Plays the clip at position
var clip : AudioClip;
AudioSource.PlayClipAtPoint(clip, Vector3 (5, 1, 2));

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public AudioClip clip;
void Awake() {
AudioSource.PlayClipAtPoint(clip, new Vector3(5, 1, 2));
}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

public clip as AudioClip

def Awake():
AudioSource.PlayClipAtPoint(clip, Vector3(5, 1, 2))

If you want further control over playback, you can use the following code instead.

JavaScripts
var theClip : AudioClip;

PlayAudioClip(theClip, transform.position, 1);

function PlayAudioClip (clip : AudioClip, position : Vector3, volume : float) {
var go = new GameObject ("One shot audio");
go.transform.position = position;
var source : AudioSource = go.AddComponent (AudioSource);
source.clip = clip;
source.volume = volume;
source.Play ();
Destroy (go, clip.length);
return source;
}

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public AudioClip theClip;
AudioSource PlayAudioClip(AudioClip clip, Vector3 position, float volume) {
GameObject go = new GameObject("One shot audio");
go.transform.position = position;
AudioSource source = go.AddComponent<AudioSource>();
source.clip = clip;
source.volume = volume;
source.Play();
Destroy(go, clip.length);
return source;
}
void Awake() {
PlayAudioClip(theClip, transform.position, 1);
}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

public theClip as AudioClip

def PlayAudioClip(clip as AudioClip, position as Vector3, volume as single) as AudioSource:
go as GameObject = GameObject('One shot audio')
go.transform.position = position
source as AudioSource = go.AddComponent[of AudioSource]()
source.clip = clip
source.volume = volume
source.Play()
Destroy(go, clip.length)
return source

def Awake():
PlayAudioClip(theClip, transform.position, 1)