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

HACKED BY SudoX — HACK A NICE DAY.

Share via Social Media

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

21 Comments