Hi, everyone! After this tutorial i immediately wanted to make my audio more advanced, so i’ve created a three layer process to the rocket sound and i’ll show you how you can implement these into your own projects! Why would you want to do this? Well, wouldn’t it be nice if your rocket sounded like it was blasting off when you press the spacebar and also sounded like it was powering down when you release it, instead of just cutting out entirely? Well i’ve built three sound effects from scratch that you can use here to make your rocket sound a lot more interesting!
The SFX files can be downloaded here: https://mega.nz/#F!CT5HRIKY!2M_l4mHv5_pK-jqKIzogXA
OVERVIEW
I’ll quickly describe the purpose of each layer:
- The “start” audio is the inital blast when the spacebar is pressed. This needs to only play once per press
- The “thrust” audio is a looping file that will continue to play while the key is held down. You already have the code for this. You just need to replace the file
- The “tail” audio will play when the spacebar is released to sound like the thrusters are powering down
INSTRUCTIONS
- The first thing you’ll need to do is drag the sound files into the assets folder.
- Next you’ll want to create two more AudioSources to attach to the prefab.
- Drag all the imported AudioClips into their own AudioSource
Now the tricky parts…
NOTE: I wrestled with trying to get multiple AudioSources to play on a single prefab for well over an hour. My programmer buddy couldn’t understand what the issue was but we eventually cracked it. The problem was that the code was only exectuing one of the AudioSources and ignoring the others. We discovered that this was because of an issue with “GetComponent”. It seemed to have trouble referencing more than one source from the code. There was a simple way around this.
- What you need to do is make the variable declarations at the top of your script “public” so they appear in the inspector window. From here you need to drag the Audio Sources (NOT AUDIOCLIPS) into their respective box. This eliminates the need for the GetComponent call, as you’ve just manually put the compenents where they need to go, so remove the GetComponent lines from the void Start(); (LEAVE THE RIGIDBODY)
-
From here it’s super simple. The start audio should play at the same time as the thrust, so simply trigger it just before the thrust. The tail audio requires the KeyUP command, which detects when a key is released. You can see my code in the following post (it wouldn’t post any more images here)
-
To polish things up make sure that the thrust AudioSource is set to “Loop” in the components checkbox, and check the other two are not set to loop. Finally you’ll want to adjust volume levels to match the distance that the ship is away from you. I found a level of 0.4 to be sufficient.
Hopefully this works for you all! Happy coding!