Sounds are very important in a game if a sound effect is played at the wrong time, it can ruin the whole gameplay experience. You need the right sound to play at the right time. If you are not sure where to find sound for your game use our Game asset finder or hire a sound designer. Unity has many tricks to play sound in synchronous with your gameplay. In this tutorial we will see how to play an audio clip and how to synchronize it with your gameplay.
How Play audio clip on scene start
- Create a new Empty object in hierarchy window by clicking Create>Create Empty.
- Go to the inspector window and click on Add Component.
- Search for AudioSource and click Add
- Drag and drop your audio file to the AudioClip dialog box under AudioSource in the inspector window.
- Check Play On Awake to play the audio when the scene loads.
- Check Loop option if the audio is required to play continuously.
How to play Audio clip on Trigger
- Create a new script called Play_audio.
- Copy and paste the code below.
- Attach your script to the gameobject with the audio source.
- Assign your clip to the source.
- Disable Play on Awake in the audio source.
- Play the game and the audio clip will play when the condition in the script is met.
Below script uses time delay as the condition. You can add your required condition to the script.
public class Play_audio : MonoBehaviour
{
bool ready=false;
float delay=5f;
AudioSource aud;
bool audioplayed=false;
// Start is called before the first frame update
void Start()
{
aud=GetComponent<AudioSource>();
StartCoroutine("Delaythis");
}
IEnumerator Delaythis()
{
yield return new WaitForSeconds(delay);
ready=true;
}
// Update is called once per frame
void Update()
{
if(ready && !audioplayed)
{
aud.Play();
audioplayed=true;
}
}
}
How to play an Audio clip at point
- Create a new script called play_at_point.
- Copy and paste the below code to the script.
- Add the script to any gameobject in scene.
- Assign audio clip to the script in the inspector window.
- Assign the position to at which the clip has to be played.
- Play the game and the audio clip will play at the assigned point.
using UnityEngine;
public class play_at_point : MonoBehaviour
{
public Vector3 point;
public AudioClip aud;
void Start()
{
AudioSource.PlayClipAtPoint(aud,point);
}
}
How to play an audio clip without audio source
- Create a new script.
- Copy and paste the code from play audio clip at point section.
- Attach the scrip to your gameobject.
- Set position to (0,0,0).
- Press play and the audio will play without an audio source.
Disadvantages of playing audio clip without an audio source
- You cannot control the audio play settings.
- You cannot stop the audio.
- No control on sound effects.
- Audio will be looped continuously.
How to play audio clips in sequence in Unity
- Create a new script.
- Copy and paste the below code on to the script.
- Attach script and AudioSource to an empty game object.
- Uncheck Play on Awake.
- Assign the number of clips to the public variable in the inspector.
- Drag and drop your clips to the script.
- Now all your audio clips will play in sequence.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
public class Play_audio : MonoBehaviour
{
public Vector3 point;
public AudioClip[] Clip;
AudioSource aud;
float delay_time=0f;
async void Start()
{
aud=GetComponent<AudioSource>();
foreach(AudioClip n in Clip)
{
await Task.Delay((int)delay_time*1000); //Task.Delay input is in milliseconds
playaudio(n);
}
}
void playaudio(AudioClip n)
{
aud.clip=n;
aud.Play();
delay_time= n.length+1;//1 second is added to cater for the loading delay
}
}
Above script uses async and task.delay to play the audio at a time difference.
Went through a lot of blogs, found the right answer here. Really thank you! Cool.