The Game of Life and How to Play It Audio

I work in Unity a lot and, while I'm always learning, it's easy to forget that, for a lot of people using the software for the first time, the most useful techniques to know are often the most basic ones.

Once you know the basics it's much easier to learn more, to experiment and to develop more advanced ideas. So, to help, I've decided to put together an easy guide that covers the basics of playing audio and sounds in Unity, along with some real examples that you can use in your project.

So… How do you play audio in Unity?

There are several different methods for playing audio in Unity, including:

  • audioSource.Play to start a single clip from a script.
  • audioSource.PlayOneShot to play overlapping, repeating and non-looping sounds.
  • AudioSource.PlayClipAtPoint to play a clip at a 3D position, without an Audio Source.
  • audioSource.PlayDelayed or audioSource.Playscheduled to play a clip at a time in the future.
  • Or by selecting Play on Awake on an Audio Source to play a clip automatically when an object loads.

Each method has its own unique benefits and use cases and in this article I'll be explaining how each one works in detail as well as providing some examples that you can use in your game.

What you'll find on this page

Methods for playing audio and sounds in Unity

  • How to use Play
  • How to use Play One Shot
  • How to use Play Clip at Point
  • How to use PlayDelayed and Play Scheduled
  • How to use Play on Awake
  • Ready-made options

Useful examples of audio triggers

  • How to play a random sound
  • How to play a repeating sound
  • How to play a sound when pressing a button
  • How to start music or audio when entering an area (3D & 2D)
  • How to play a sound when colliding with an object (3D & 2D)
  • How to play a sound when destroying an object

How to play audio in Unity

Here's a quick overview of the five different methods and how they work.

 How to use Play

          //Plays an Audio Source audioSource.Play();        

Call Play from a script to start an Audio Source:

          public class PlayAudio : MonoBehaviour {     public AudioSource audioSource;      void Start()     {         audioSource.Play();     } }        

Calling Play will trigger whatever Audio Clip is set on the Audio Source. It's the most common method of triggering audio and is most suitable for playing looping sounds and music.

This is because some other methods, such as Play One Shot, cannot trigger looping sounds, and also because using Play will only allow one clip to be played at a time from that Audio Source.

Calling Play on an Audio Source that is already playing will stop the Audio Source and start the clip again.

How to use Play One Shot

          //Plays a specific clip on an Audio Source once audioSource.PlayOneShot(AudioClip audioClip, Float volumeScale);        

Play One Shot is possibly the lesser-known method of triggering a sound on an Audio Source but is extremely useful for triggering sound effects.

          public class PlayAudio : MonoBehaviour {     public AudioSource audioSource;     public AudioClip clip;     public float volume=0.5f;      void Start()     {         audioSource.PlayOneShot(clip, volume);     } }        

Play One Shot is called on the Audio Source and takes a parameter for the Audio Clip to play. This means that you must specify what clip you would like to play when you call PlayOneShot.

If, however, you just want to play whatever clip is on the Audio Source (like you would with play) then you can just pass a reference to that clip like this:

          audioSource.PlayOneShot(audioSource.clip, volume);        

You can also set an optional volume scale when calling Play One Shot. This takes a float value between 0 and 1 and will adjust the volume of the clip relative to the volume of the Audio Source.

This is great for variation because it means that you can pass in a randomised volume value, or play two different clips at different volumes, without changing the overall volume of the Audio Source itself.

Play One Shot is ideal for sound effects, particularly repeating sounds (for example gunfire). Triggering Play One Shot on an Audio Source that's already playing will overlap the sounds (instead of interrupting it, like it would with Play).

This makes Play One Shot especially suitable for repeating sounds but be careful. Try not to allow too many sounds to overlap all at once.

Despite only playing from one Audio Source, two sounds triggered via Play One Shot will count as two 'voices'. And even though the maximum voice limit can be increased from the default 32 voices, different platforms have different limitations, so it's good practice to try to keep the total voice count as low as possible to avoid sounds being culled.

  • Find out more about Play One Shot

How to use Play Clip At Point

          //Plays a clip once at a specific position, without an Audio Source AudioSource.PlayClipAtPoint(AudioClip clip, Vector3 playPosition, Float volume);        

Play Clip at Point works in a similar way to Play One Shot with one big difference. It doesn't require an Audio Source to work.

This is great for one-off sounds as you won't need to add an Audio Source component to an object, you can just fire a sound once, from a specified position in the Scene, without any fuss.

          public class PlayAudio : MonoBehaviour {     public AudioClip clip;     public float volume=1;      void Start()     {         AudioSource.PlayClipAtPoint(clip, transform.position, volume);     } }        

Note the capital A in 'AudioSource', as Play Clip at Point uses the Audio Source class, instead of an instance of an Audio Source.

You also have to pass in a position that the Audio Clip will play from. In this example I've used the position of the object that this script is attached to.

So how does this work exactly?

Play Clip at point Unity

When using Play Clip At Point, Unity creates a temporary Audio Source to play the clip from.

Behind the scenes, Unity creates a new, temporary Audio Source to play the clip from and disposes of it once the clip is finished. In the editor, you'll see a new Game Object appear and disappear when this happens. This makes Play Clip at Point especially useful when using an Audio Source Component isn't an option.

For example, when playing a sound when an object is destroyed.

If the Audio Source is on the object that's being destroyed the sound will normally be cut off, or it may never get a chance to start, as the Audio Source now longer exists.

Using Play Clip at Point instead allows the sound to continue after the object is destroyed via a temporary Audio Source that is neatly disposed of once the sound is finished.

However, there is a catch…

The downside of using Play Clip at Point

Because the Audio Source that the clip is played from is created at run time, you don't get the option to change any of the Audio Source's settings. The Audio Source is created using default settings, with the exception of spatial blend which will be set to fully 3D.

This also means that you can't use Play Clip at Point with an Audio Mixer. The output will go directly to the Audio Listener, as it does by default.

If you do need to change any of the Audio Source's settings there are ways you can get around this (by creating your own modified version of the Play Clip at Point functionality). However, in that scenario, it's probably going to be much, much easier to simply use a regular Audio Source.

How to use Play Delayed and Play Scheduled

Play Delayed and Play Scheduled allow you to play an Audio Source in the future, in two different ways.

Play Delayed

          //Plays an Audio Source after a delay AudioSource.PlayDelayed(float delay);        

The first, Play Delayed, takes a delay in seconds and is the simplest method for delaying an Audio Clip.

          public class PlayAudio : MonoBehaviour {     public AudioSource audioSource;     public float delay=4;      void Start()     {         // Plays an Audio Clip after 4 seconds         audioSource.PlayDelayed(delay);     } }        

This method is easy to use and is ideal for quickly changing the timing of an Audio Source.

Examples of when you would use this include delaying the start of music, sound effects that occur after the event that triggers them, like lightning and thunder for example, and other timing tweaks and randomisations.

Play Delayed is great for simple timing, but it is not suitable for more precise timekeeping, like stitching two clips together or beat matching audio clips.

For that, you need PlayScheduled.

Play Scheduled

          //Plays an Audio Source at a precise time AudioSource.PlayScheduled(double timeToPlay);        

Play Scheduled works a little differently to Play Delayed, but it's incredibly precise.

This is due to the fact that it operates using the actual audio engine time and takes a highly accurate double value, instead of a float.

To use it, you need to specify the time the Audio Source should start, not a delay. This corresponds to the AudioSettings.dspTime, which is the number of seconds the audio engine has been running.

Play Scheduled is ideal for precise audio timing tasks, like queueing audio clips to play exactly after one another, or for beat matching.

The below example hows how to queue a clip to play seamlessly after another clip has finished.

          public class PlayAudio : MonoBehaviour {     public AudioSource audioSource1;     public AudioSource audioSource2;      void Start()     {         audioSource1.PlayScheduled(AudioSettings.dspTime);         double clipLength = audioSource1.clip.samples / audioSource1.clip.frequency;         audioSource2.PlayScheduled(AudioSettings.dspTime + clipLength);     }        

There's a lot that you can do with PlayScheduled. So much that I wrote a huge guide on just that, with examples of how to queue up clips, beat match audio and how to end loops with musical stings.

  • How to queue audio clips in Unity: The ultimate guide to Play Scheduled

Using isPlaying when scheduling audio won't work

When scheduling audio using either method, the Audio Source is considered to be playing as soon as it is scheduled and until after it has finished. This happens as soon as the Audio Source is queued, not when the sound starts, so isPlaying will be true, even if no sound is being made yet.

This means that, when scheduling audio, you cannot check if the audio has started or stopped using isPlaying.

How to use Play on Awake

Play on awake is the easiest way to trigger a sound, as it requires no scripting whatsoever.

Play on Awake control in Unity

Play on Awake is enabled by default.

Enabled by default, Play on Awake will start the Audio Source as soon as the object is enabled. This will usually be the start of the scene if the Game Object is already enabled or, if it is disabled, Play on Awake will fire as soon as the Game Object, or the Audio Source, becomes active.

This is useful for automatically starting audio when an object loads, such as music at the start of a scene or a sound that plays when an object is initialised.

For example, to add a sound when a new object is created, simply add an Audio Source component to the prefab of that object and make sure Play on Awake is checked. Then, when the object is added to the Scene, the sound will play.

For more information on the different methods that can be called from an Audio Source, try Unity's official documentation.

Ready-made options

While Unity's built-in options for handling audio are much better than they used to be, if you want easy access to advanced audio features (or even if you just want to save some time) you might want to consider an audio manager plugin from the Unity Asset Store.

Master Audio, for example, has long been the gold standard in audio manager assets. Fully featured and well supported, it offers advanced features, such as audio occlusion, as well as basic conveniences, such as solutions for adding sounds to odd-shaped objects (such as rivers for example). The Multiplayer version extends the asset further, making, normally challenging, multiplayer audio, much easier.

For larger developers, Fmod and Wwise are powerful audio integration options, both of which are compatible with Unity (Fmod has a Unity plugin which you can download from the Asset Store here). While powerful, their advanced features are offered under licensing tiers which are, much like Unity, based on revenue and production budget. For more, check the official websites here: Fmod, Wwise.

Audio trigger examples

Now you know the different methods for triggering audio in Unity it's time to put them to work.

Below you'll find some examples of common audio triggers that you can copy and paste in to your project.

How to play a random sound

What's the best way to select a random sound in Unity?

First, create an Audio Source and an array of Audio Clips.

          public AudioSource audioSource; public AudioClip[] audioClipArray;        

Back in Unity, add the different Audio Clips to the Array.

Audio Clip Array in Unity

An Array of Audio Clips

Next, write a function, with an Audio Clip return type, to select one of the clips at random.

          AudioClip RandomClip(){     return audioClipArray[Random.Range(0, audioClipArray.Length)]; }        

Then whenever you want to use a random Audio Clip, pass in the function where you would normally pass in the clip name:

          void Start() {     audioSource.PlayOneShot(RandomClip()); }        

How to trigger a random sound, with no repeats

To prevent the same sound triggering twice in a row, store the previous sound in a variable and check against it with a while statement, like this:

          public class PlayAudio : MonoBehaviour  {    public AudioSource audioSource;   public AudioClip[] audioClipArray;   AudioClip lastClip;    void Start()    {     audioSource.PlayOneShot(RandomClip());   }    AudioClip RandomClip()   {     int attempts = 3;     AudioClip newClip = audioClipArray[Random.Range(0, audioClipArray.Length)];      while (newClip == lastClip && attempts > 0)      {       newClip = audioClipArray[Random.Range(0, audioClipArray.Length)];       attempts--;     }      lastClip = newClip;     return newClip;   } }        

How to play a repeating sound

Here's an example of how to trigger a sound repeatedly in Unity, in this case for a machine gun that fires every 0.25 seconds.

I'm also using the Audio Clip array from the last example to use a random sound every time the gun is fired.

          public class PlayAudio : MonoBehaviour {     public AudioSource audioSource;     public AudioClip[] audioClipArray;      public float timeBetweenShots = 0.25f;     float timer;      void Update()     {         timer += Time.deltaTime;         if (timer > timeBetweenShots)         {             audioSource.PlayOneShot(RandomClip());             timer = 0;         }     }      AudioClip RandomClip()     {         return audioClipArray[Random.Range(0, audioClipArray.Length-1)];     } }        

How to play a sound when pressing a button

Unity has a built in method for triggering events when a button is pressed and this includes an option for playing an Audio Clip.

Just create an On Click event trigger, drag a Game Object to the object field and select PlayOneShot(AudioClip) from the Audio Source section of the drop down menu.

Play a sound from a button in Unity

Drag an object with an Audio Source to the button On Click event, then select one of the Play options. Easy!

Once you've selected Play One Shot, select an Audio Clip to play in the new field that appears.

By using Play One Shot, it's possible to have different buttons play different sounds using only a single Audio Source.

However, you can also select from Play, or PlayDelayed, just like when playing from a script.

How to start music when entering an area (3D & 2D)

How can you create a music trigger in Unity that fires when the player enters a specific area?

First create the Trigger:

  1. Create a shape, such as a cube that will act as the trigger area in the scene.
  2. Remove the mesh renderer if there is one, you won't need it for this.
  3. On the Collider component make sure Is Trigger is set to true.
  4. If the Player object doesn't have one already, you'll need to add a Rigidbody component for this to work.
  5. Finally, on the Player object, select the Player Tag.
    Player Tag in Unity

Next, add this script to the trigger area:

          public class PlayOnCollision : MonoBehaviour {     public AudioSource audioSource;      void OnTriggerEnter(Collider other)     {         if (other.tag == "Player" && !audioSource.isPlaying)         {             audioSource.Play();         }     } }        

How to create a trigger sound in 2D

To create the same effect in a 2D project, simply use the 2D Collider and 2D Rigidbody, making sure to update the script to match:

          void OnTriggerEnter2D(Collider2D other) {     if (other.tag == "Player" && !audioSource.isPlaying)     {         audioSource.Play();     } }        

How to play a sound when colliding with an object (3D & 2D)

Just like entering an area, you can trigger a sound when two objects collide, for example if an object falls on the ground.

Also like entering an area, the object will need to have a Rigidbody component for this to work.

Place this script on the moving object:

          public class PlayOnCollision : MonoBehaviour {     public AudioSource audioSource;      void OnCollisionEnter(Collision collision)     {         audioSource.Play();     } }        

To prevent minor collisions triggering the sound, it's possible to only play the sound for larger collisions, by checking the velocity of the collision:

          public class PlayOnCollision : MonoBehaviour {     public AudioSource audioSource;      void OnCollisionEnter(Collision collision)     {         if(collision.relativeVelocity.magnitude > 1)             audioSource.Play();     } }        

Or, for a more natural sounding collision, you can vary the volume based on the force of the collision.

          public class PlayOnCollision : MonoBehaviour {     public AudioSource audioSource;     public float maxForce = 5;      void OnCollisionEnter(Collision collision)     {         float force = collision.relativeVelocity.magnitude;         float volume = 1;          if (force <= maxForce)         {             volume = force / maxForce;         }         audioSource.PlayOneShot(audioSource.clip, volume);     } }        

The max volume value decides what force of impact will produce the loudest sound. A value that exceeds the max force will simply play at full volume.

How to create a collision sound in 2D

The same method can be used to create a collision sound effect in a 2D game using 2D Colliders, a 2D Rigidbody and updating the script classes to match, like this:

          void OnCollisionEnter2D(Collision2D collision) {     audioSource.Play(); }        

How to play a sound when destroying an object

To play a sound that continues after an object has been destroyed, one option is to use Play Clip at Point, like this:

          public class PlayOnDestroy : MonoBehaviour {     public AudioClip audioClip;      void DestroyObject()     {         AudioSource.PlayClipAtPoint(audioClip, transform.position);         Destroy(gameObject);     } }        

This works by creating a temporary Audio Source that plays at the object's last position, even though the original object is gone.

Now I'd like to hear from you…

Hopefully, these tips and examples will help you play audio in Unity more easily.

At the very least you now know many of the basic options available for playing audio in Unity, making it easier to build more complex audio systems.

But now I want to hear from you…

How will you use these examples in your game? Or maybe your project needs something different, that's not in this article. What challenges did you face when starting with the basics of audio in Unity?

Let me know by leaving a comment below.

Get Game Development Tips, Straight to Your inbox

Get helpful tips & tricks and master game development basics the easy way, with deep-dive tutorials and guides.

My favourite time-saving Unity assets

Rewired (the best input management system)

Rewired is an input management asset that extends Unity's default input system, the Input Manager, adding much needed improvements and support for modern devices. Put simply, it's much more advanced than the default Input Manager and more reliable than Unity's new Input System. When I tested both systems, I found Rewired to be surprisingly easy to use and fully featured, so I can understand why everyone loves it.

DOTween Pro (should be built into Unity)

An asset so useful, it should already be built into Unity. Except it's not. DOTween Pro is an animation and timing tool that allows you to animate anything in Unity. You can move, fade, scale, rotate without writing Coroutines or Lerp functions.

Easy Save (there's no reason not to use it)

Easy Save makes managing game saves and file serialization extremely easy in Unity. So much so that, for the time it would take to build a save system, vs the cost of buying Easy Save, I don't recommend making your own save system since Easy Save already exists.

The Game of Life and How to Play It Audio

Source: https://gamedevbeginner.com/how-to-play-audio-in-unity-with-examples/

0 Response to "The Game of Life and How to Play It Audio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel