Unity - Play Sound Effect During Collision (OnCollisionEnter2D) | EASY Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone it's rafael from xx raphael productions and today i'm going to show you how to play a sound effect during a collision in unity okay so this is simply um a project that i have so i'm going to go ahead and play it right now with the play button and the goal is that if our yellow circle touches the green block we want a sound effect to play okay so we're going to put this volume up actually so as of right now there is nothing but we're going to do it so the goal is right over here actually the sound should play when the yellow circle touches the green block all right so it's quite simple and let's get right to it so i've already added in my sound effect here it's called explosion sound and let's go ahead and get right um we're going to set it up so over here in the hierarchy over here right click and then um go to create empty and this will create an empty game object and then once you do this you drag the explosion sound or whatever effect you have drag it into that empty game object and that should add a component called audio source um the only thing we need to change is this over here that says play on awake you want to make sure you untick that because play on awake means it will play immediately as soon as your game loads up and what we want to do is only play when we collide with the obstacles so untick that and then when i call this game object whatever you want i'll simply call mine source audio okay so that's what we um named it as and then afterwards so right after that if you already coded your object to move we're going to go ahead and um open up my canvas so this is the player that is the yellow circle and here i'm going to open up my script so movement of player okay that is a script that i used um to code the object to move around so this is where we'll also add the audio on the audio source so you can see that i already have the code over here ignore this i actually listed down here because that is only the code for moving the object so what we have to worry about is the code for the sound effects all right so what we're going to do now before we enter in some code though we have to add in the audio source so i'm going to go in over here on top of the start on method public audio source and i'm gonna call this audio player all right make sure that's saved and then right afterwards we're gonna do the um code for when we have a collision okay so to do this do public void on collision enter 2d then could do collision 2d collision now it is case sensitive so please make sure you've entered that incorrectly or else it will not work all right so once you've got that down um we'll do an if statement but before we do an if statement for this we have to make sure that um the tag has been set up so to do this head over back to unity like the actual game engine uh wait for that to open up all right there we go so what we have to do now is kind of click on the obstacle or whatever like you're trying to collide with and now we are going to make sure that the tag is all set up all right so you i already made my own tag scene transition but you can add a new tag if you want to i'm going to go ahead and do that right now just to make on to go through the process click the plus button down here now if you have no extra tags this should be empty but you can click the plus symbol and let's call this collision tag all right so simply just like that and then we will um set this to that tag all right so you can see i have the new tag collision tag okay so now once you do that um we can go ahead right back into visual studio code and make sure it is k wait i'm gonna get rid of this extra window so uh the reason why i'm gonna make this like this is because i wanna see the capitalization so capital c and t has to be like it is case sensitive as well so now we can do our if statement so do if collision the game object dot tag equals equals and then we called ours collision tag so here now in the if statement um whatever you called your audio source type audio player dot play oh this should be wait capitalized i'm sorry the p should be capitalized before you can play that's why we have like an error message down here head over to um your player so you can click on that or you can find it over here in the canvas it should highlight the um circle and then here in the movement of player script which is the script we just edited you can see audio um player is empty right now it's just none and then audio source now we're gonna drag in source audio right into that box over here and then now we should be able to play the sound okay so let's go ahead and um just make sure you've got that down first okay like um when we entered this in earlier we forgot to capitalize the p so just make sure you fix that and then now we can go ahead and play the game so now this should give off a sound effect as we collide all right so i'll put the volume up and then yeah there we go um so immediately as soon as the circle touched the green block it ex it played the explosion sound alright so yeah that is simply how you do it in a collision um if you want to see my other video where i kind of made a tutorial on how to play a sound effect using a button i will link that down in the description below but if you have any extra questions always feel free to comment down below thank you very much for watching and i will see you soon [Music] foreign [Music]
Info
Channel: xxRafael Productions - Rafael Vicuna
Views: 31,892
Rating: undefined out of 5
Keywords: unity, onCollisionEnter2D, Collision2D, unity c#, unity3d, xxrafael productions, play sound effect in unity, unity how to play sound, c# programming tutorials, coding your own games
Id: UVI6bwldPio
Channel Id: undefined
Length: 6min 11sec (371 seconds)
Published: Sun Mar 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.