Controlling Sound In Unity - (In 5 Minutes!!)
ฝัง
- เผยแพร่เมื่อ 3 ธ.ค. 2024
- This tutorial is a little long, but it really only made sense when you could see/hear everything together. This is everything I normally use on a day-to-day basis regarding Sound in Unity - Hope it helps!!!
If you enjoyed this video, I have a small 1$ Member perk available for anyone who would like to help us save up for Facial Motion Capture :)
(Just click the "Join" Button next to "Like"!)
/ royalskies
Twitter at: / theroyalskies
Free Rigged Blender Male & Female Base Model:
www.theroyalsk...
Blender MODELING SERIES: • ENTIRE Low Poly Blende...
Blender RIGGING & ANIMATION SERIES:
• Blender 2.82 : Charact...
T-SHIRT LINKS HERE:
teespring.com/...
If you're a gamer, please check out my new game on steam! It took over 3 years to create and has thousands of hours and heart put into it :)
store.steampow...
As always, thank you so much for watching, please have a fantastic day, and see you around!
Royal Skies -
-------------------------------
I make no claim over the following footage as it belongs to their corresponding content creators and can be found below:
Copyright Disclaimer: Under Section 107 of the Copyright Act 1976, allowance is made for "fair use" for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational or personal use tips the balance in favor of fair use.
ALSO, there's one more important command you should know. 'mySound.Play();' This is how you activate a sound that is not checked for "Play On Awake" - It works really well with "OnTriggerEnter"! Code Below :
{
public AudioSource mySound;
float life;
void Start()
{
life = mySound.clip.length;
transform.parent = null;
mySound.pitch = Random.Range(.5f, 1.5f);
}
void Update()
{
life -= Time.deltaTime;
if (life
@RoyalSkiesLLC, don't feel bad that this wasn't quick and snappy. This video concicely fed us all of the necessary information compared to most people leaving out the very important stuff just to be quick and snappy rather than proficient and necessary. I and many like me, cherish every video you produce. Thank you.
Side note: my project owner needs to see this. It'll help quite a bit of the problems that I have been trying to explain how to fix.
Really informative video. Was able to learn quite a bit. The 3d Spatial Blend stuff especially, as I'd only really tried working on 2d games before.
That said, I prefer my method for calling sounds. It's based on the idea of object pooling & the Sega Genesis.
I was having issues with sounds being too loud & muddy, from too many playing at once.
Essentially, you have a sfx manager that everything(except the player) has to call to play sounds.
The SFX manager will have two lists - 1 for empty audio sources, and 1 to determine how long since they've been called.
So it'll loop through the age list to find the first one that isn't playing, or is the oldest. How many audio sources you have, will determine how many audio "channels" you have.
After it finds an audio source, it'll set its position to the transform location of the caller & play the new sound.
In this way, you're not creating/deleting a bunch of audio sources - and there's a limit to how many can play at once, so you can fine tune how muddy it can potentially get.
In my SFXManager, I also have options for repeating sounds - like machine guns/ burst weapons.
This is incredibly simple, because you can simply establish generic Coroutine variables, and then assign active coroutines to them. This lets you start and stop them whenever.
That said... with a 3d game, the only real benefit to the system is the object pooling part, because of Spatial Blend.
Yess niceeee
I don't know anything about unity but i think its time to learn unity if you're making tutorial videos for it
Your style of explaining things never leaves me confused and the added benefit of getting straight to the point is what keeps me coming back. Literally every question I had about getting started with the audio system was explained perfectly. You're the best!
Never really accepted the fact that a 3D game needs multiple audio sources until I watched this. Thanks!
I've not looked into sound in my Unity endeavors, so good to have this ready for when I do. Nice to see that Unity's default sound system is fairly robust.
It's amazing the fact of ALL of your videos has something new to learn even the tittle is about a component or thing that i already knows.
Have a faaaaaaantastic day too!
I'll, see you araawnd.
Was waiting for this one!
used unity for a while and didn't know what those settings did lol. Good to know!
Usually helps to play around with settings of anything to learn it :)
Wish they had this in Blender.
If a script needs a component that's found on the same game object as the script itself, such as the Audio Source here, you don't need to make it a public field that has to be set up in the Inspector. Instead, make it a private field, and in your Start method, say
mySound = GetComponent();
at some point before it gets used. (In this case, that would mean it goes on the very first line of Start.) If you want to get extra fancy about it, you can put an Attribute on the script to enforce this relationship. On the line immediately above the "public class" line, say
[RequireComponent(typeof(AudioSource))]
to tell Unity that any game object you put this script on also needs an AudioSource. Now if you put this script on an object that doesn't have one, it will automagically create the AudioSource for you.
GetComponent() is bad for performance, so it's better to just drag it in in the inspector, and use GetComponent() only when necessary. I mean in this case, it's pretty much inconsequential...
@@thedude4039 Using GetComponent *repeatedly* is bad for performance. If you only do it once on Start, that won't cause any noticeable performance hit.
@@masonwheeler6536 True.
yes
Amazing as always. I also have one question, can you make a tutorial how to controll the animations? I think it would be very useful
Yes, the Patreon and Members have voted for that tutorial series next month :)
@@TheRoyalSkies Heck yeah! I can't wait for you to say "Soooow, you made some great animations in blender, but you wanna know how to play and blend between them in unity, NAT A PRAAAABLUM!!!!!!!!!!!!!!!!".
@@thedude4039 I can just imagine the voice xD
@@Ikxi Same.
Thanks bro
I would make mySound private and assign GetComponent() to it at Start or Awake, also it would be more accurate to name it myPlayer or something but that's just details, thought I'd throw that out there.
If you want to change the sound for X or Y reason, you can change the clip, any and all supported audio files you put in your project are recognized as AudioClips, you can very easily just put in your script some AudioClip variables, and just change audioSource.clip whenever you want.
One thing that changing clips can be pretty useful for is music for example, you may have a track that can be looped, but not from begining to end, rather there's a certain point in the track that's the loop start, and there can also be a loop end, where the track plays from begining until the loop start, then it keeps playing but once it reaches the loop end, the track plays again, except from the loop start, not the actual start, and when you're no longer gonna play that song you can play from the loop end to the actual end and destroy the object if you need to.
Some amazing examples of this kind of system are found in Devil May Cry 5 and Metal Gear Rising Revengeance, where the track will jump to certain parts and even overlay the vocals in key moments, but that might be too much for starting, and there's some interpolation algorithm to those that makes them work regardless of at what part of the song is currently playing.
Anyway, here's some code to do simple music looping:
{
public AudioClip
start,
loop,
end;
AudioSource source;
bool ending;
void Start()
{
source = GetComponent();
source.clip = start;
source.loop = false;
source.Play();
}
void Update()
{
if(!source.isPlaying)
{
if(!source.loop&& !ending)
{
source.clip = loop;
source.Play();
source.loop = true;
}
else if(ending && !source.loop)
Destroy(gameObject);
else if(ending)
{
source.clip = end;
source.Play();
source.loop = false;
}
}
}
public void EndSong() => ending = true;
}
Here, to end the song you just call the EndSong() method from somewhere, there's an argument to be made to make this work with a single clip and just jump around to certain times in the song, but I think that just complicates the usage a bit.
Does it make no difference performance wise having a lot of "getcomponent" in the start method? I thought I heard that if you have lots of objects calling getcomponent in start the game may take longer to start? However, I never bothered to verify if this was true or not -
@@TheRoyalSkies I never really tested it myself, but I don't see why it would be a problem in methods that only get called once like Start or Awake, I don't know the details because I don't have a job programming the C# APIs for Unity, but from what I've seen in just metadata VS shows me when hovering on stuff, it seems what GetComponent does is iterate through each component in the object in question, and if the type matches then we know that's the component we want, if nothing matches then return null.
Overall, it doesn't really hurt anything, really only be mindful of it if you're gonna be doing it in Update methods, and even then, you usually don't do it each cycle, you register it based on a condition and never touch it again unless you need to update the component, like say get it from a different object.
Basically it doesn't really matter much unless you're gonna be calling it a lot, like once every frame for some reason, or if you wanna get a lot of components, and be mindful that in programming, when we say a lot, we usually mean numbers that range from dozens to even billions depending on the data structure, and since in this case, we're talking about a function that simply looks for an object based on simply the type, and returns just a mere reference to it, which at the end of the day is really just a glorified pointer, C style, and that's just a single 64 bits integer worst case scenario if your app is running with the amd64 or x86_64 instruction set, that's tiny in this context.
Now of course this depends a lot on Unity's implementation under the hood, but again, it only really hurts if you're gonna be calling it a lot in most cases.
"Hope that wasn't too long." My dude, it was 5 minutes.
2:41 *How CLOSE you have to be*
5:05 "mySound.loop" is said but you write "Play" instead of "loop".
Which one would it be?
Oh, the code is .loop. I wrote .Play just because I wanted to show you guys .Play() because that's the only one that looks different from the others. I should have specified that-!
When you put a sound through a loop, there is a clicking sound when it repeats (you got to listen closely to hear it). Is there a way to loop the sound without hearing the clicking noise?
I would think this should be resolved entirely in making the sound itself a seamless loop.
The easiest way to do this (but experiment further for your case) is to render out the sound, make a new empty sound in whatever software you are using, and load that sound into it twice: one all the way at the start, and the other one starting exactly at the looping point. Then you fiddle with fade-outs and fade-ins and perhaps even change the looping point, until you don't hear a clicking sound anymore when you play the double sound. Then you the second part of the first sound together with the second sound over and have it replace the second part of the second sound, so you have a triple loop, with identical transitions. Now, all you have to do is choose a starting point somewhere during the first transition (that could be the same as you had before this process, but it's possible that sounds too abrupt). That same point should become your ending point, only in the second transition. You render out this part of the sound exactly (important to be precise) and use that in Unity. No more clicking.
This means that your sound is not designed to loop. If you open the sound in an audio editor and zoom in far enough, you'll see that the whole thing is made of a bunch of points that go up and down in a more-or-less smooth waveform. Looping basically means that when you hit the end of the wave, feed the beginning of the wave immediately into the audio playing hardware and keep on going. But if there's a big enough difference between what the wave looks like at the end and what it looks like at the beginning, then the sound wave isn't smooth anymore at the point where it wraps. There's a sudden discontinuity there, and that's the pop you hear.
To fix this, either find a different sound that's designed to loop, or edit the one you have. Make sure that at the start and at the end, the volume is the same, the pitch is the same, and the waveform at the beginning looks like it's a continuation of the waveform at the end. (Approximately the same height, moving in the same direction up or down, etc.)
@@masonwheeler6536 This is a great way to put it, but I would add that it's still possible for the transition to feel very abrupt (even though the click might be gone) if the waveform is only just approximately at the same height and moving in the same direction. This has to do with how we hear sound - it's a bunch of frequencies coming in and out. When that waveform is at the same height and moving in the same direction, it might still suddenly explode into a bunch of frequencies that were not playing before which can sound very abrupt. I suppose the long version of both of our suggestions is: this is a sound design matter, and a good understanding of sound design will help you make sounds that loop better.
@@rensdejonge3 Yeah, like Mason said, this sound wasn't originally designed to be a loop. I just picked it because it was easy and distinct to hear. You can "technically" loop any sound, though it's recommended you use a sound designed for it -
If the sound is designed to loop, or if it isn't but you can find looping points that are similar enough, you could in theory tweak the wave itself so the end and the start meet at the same magnitude, of course you would wanna drag nearby points slightly towards the same direction too, like using proportional editing in Blender.
I mean why wouldn't you be able with a software that'll let you do that? At the end of the day, even if there's multiple sounds in the clip, the final audio is still a single wave because of wave interference in fluid dynamics.
Heck, Audacity has a feature that lets you drag the individual vertices of an audio, I don't think it's practical because of the tiny zoom at which you can do it, and also I don't *think* there's a feature like proportional editing for that, or inputting an specific magnitude so you can actually match the end and the start, but it's still a think you can do with the software.
So, conclusion, I haven't tried to do this at all, it's all theory, I'd have to make an app that does exactly what I'm thinking to see if I can get it working, but the idea's out there if someone can do something with it, and if not, maybe at some point I'm gonna try it myself.
wow at least 1 tutorial without much coding..........
SO YOU! Are gonna start making videos for UE4 instead!? ❤️ Fucking love your content bud