unity 3d student

Beginner B10 – Audio Basics


How to play sound with the Audio Source component, and call sound clips via scripting.

Code Used

Using PlayOneShot:

//Play an instance using attached AudioSource component
  1. var myClip : AudioClip;
  2.  
  3. function Start () {
  4.  audio.PlayOneShot(myClip);
  5. }

Using PlayClipAtPoint:

//Spawn a new object with AudioSource and Clip set, at a point in the 3D world
  1. var myClip : AudioClip;
  2.  
  3. function Start () {
  4.  AudioSource.PlayClipAtPoint(myClip, transform.position);
  5. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

21 Comments

    I am loving these Will. Excellent bite-sized morsels!

    • Ha! Cheers Mani 🙂

    I have a problem with the PlaySounds script. I am using a Bass Kick sound as a sound fx and when it plays through the script it sounds like a click (although it’s a bass sound). At preview it sounds right. When I use the stereo song (it’s an mp3 song), first I hear a sound effect that I haven’t load and then it starts playing the mp3….Any suggestions?

    • im not too sure but probably its due to the compression. Click on your audio file in unity. In the inspector window, try and increase the Compression. It might be the reason :3

    It works well with the script that has the PlayClipAtPoint command….

    • Excellent¡¡¡ Thanks

    Hi excellent aport im folowing every post about audio and sound , Thanks¡¡¡

  • anybody else get two click noises when the video starts

  • I used this script with c#, i wanted to actually see the gameobject with the audioSource component (so i could see the audio logo on screen), uncheck play on awake and place an audio clip for this script to work:

    using UnityEngine;
    using System.Collections;

    public class PlaySounds : MonoBehaviour {

    //public AudioClip myClip;

    // Use this for initialization
    void Start ()
    {
    //AudioSource.PlayClipAtPoint(myClip,transform.position);
    AudioSource.PlayClipAtPoint(gameObject.audio.clip
    ,transform.position);
    }

    // Update is called once per frame
    void Update () {

    }

  • How do you set it up so that the sound plays when the object its attached to collides?

    • put it inside an OnCollisionEnter() function.. see other tute.

    Hello how to play a sound when you collide with an object? on a FPS mode?

    Thanks

    • There is no fps mode! use OnControllerColliderHit and audio.PlayOneShot or AudioSource.PlayClipAtPoint and you’ll have your answer! see script reference

    I’m wondering why the second script worked. Don’t I have to instanciate the AudioSource?

    Thanks for thr greatTutorials

  • Thanks Will,

    Do you know off-hand if Unity handles doppler effect and such? I am guessing it does.

    So, if you wanted to play music that was not a 3D sound, say just a music track, would you just apply it to the same object where the listener is, the camera? Or is there something special for playing a non 3D sound source?

  • these are the best tutorials on line HANDS DOWN.
    PLEASE keep it up!

  • hello there..one question please…how i do that in boo lauguage? thanks

  • how to save audio clip to our computer in unity c#?

  • Awesome tutorial sets!! thank you so much!

    i just wanted to point out the irony of the Sound clicking at the start of the Audio tutorial. Hah!

    Really great stuff here. I can follow it, and the OP is sticking to the subject.

    Most guys blab on for 20 minutes to just show you how to click 5 buttons. This guy can teach you programming basics in 20 mins!!

    Thanks Bro!

    • Glad you enjoyed, cheers!

    For those of I having problems with .mp3 try formatting it to .wav and uncheck 3d sound

Leave a Comment