This video shows you some techniques for playing audio through C# code in Unity. We discuss randomizing the pitch and volume of the Audio Source component before you call the Play() function, and we demonstrate a use case for PlayOneShot(). We also explain a couple of ways of fading audio, the first being using Audio Mixer Snapshots, and the second using coroutines and loops.
XRTerra Links:
Programs and Courses: http://www.xrterra.com/programs
Newsletter Signup: https://www.xrterra.com/mailing-list-...
Audio in Unity: • Audio In Unity
For and While Loops: • C# For And While Loops in Unity
C# Coroutines: • C# Coroutines in Unity
00:00 Intro
00:19 Roblox oof death sound
00:43 Testing that audio is working
01:12 Audio Source documentation
01:58 Creating a new script
02:26 Referencing the Audio Source
02:55 AudioSource.Play()
03:28 AudioSource.PlayDelayed(float)
04:19 Triggering sound with checkbox
05:22 Preventing ear fatigue
05:43 New method to play sound
06:02 Randomizing pitch
06:49 Randomizing volume
07:38 Testing out randomized pitch and volume
08:09 AudioSource.PlayOneShot(AudioClip)
09:03 AudioClip type
09:22 Testing out PlayOneShot(AudioClip)
09:48 Fading in music
10:10 AudioMixerSnapshots
10:50 Setting up drum loop
11:23 Demonstrating the snapshots
12:06 Explaining transitioning to snapshots
12:50 using UnityEngine.Audio namespace
13:06 Declaring AudioMixerSnapshot references
13:28 Adding more inspector buttons
13:54 AudioMixerSnapshot.TransitionTo(float)
14:55 Assigning AudioMixerSnapshot references
15:50 Testing snapshot transitions
16:36 Snapshot use cases
16:58 Limitations of snapshot transitions
17:48 Coding prerequisites
18:20 Declaring a coroutine
19:06 Using decimals in our loop instead of floats
19:32 Declaring the for loop
20:25 Setting Audio Source volume in loop
20:36 Casting a decimal to a float
20:55 Fade in time
21:20 yield return new WaitForSeconds(float)
21:32 Setting up inspector button for fade
22:07 Inspector values
22:29 Testing out fade in
23:40 Outro
Instructor: Gregory Osborne