Sound in the Blender Game Engine
Blender 3d tutorial
To bring your game to life with sound in Blender 3D, we will go over where to find sounds on the web, how to use the sound actuator with Game Logic Bricks, and some common pitfalls involved with using sound in the game engine.
This tutorial builds off of previous game engine tutorials, specifically Movement and Animation in the Blender Game Engine. They are not required to understand the concepts here, so I am providing a starter blend that you may use: Sound in the Blender Game Engine Starter.
If you download and open the file, you'll see that I have shifted things around a bit and added some more textures to the game. The camera should have WASD controls hooked up and the platform should have the rising and lowering animation created in the previous tutorial.
Finding good sounds to fit your project can be very difficult if you don't know where to look. Lately, I have found FreeSound to be a very good resource and and the licensing terms are typically very open, which is a very big plus for me. This is where we will get our first sound.
Since we are going to be adding sound to our platform lift, I have found a nice hydraulic lift sound that I will be using. If you like, feel free to browse the site to find a sound you like.
Also, when using a sound, keep in mind Blender may not work for certain file types. Thus far, I have used MP3, AIF, WMA, and WAV files successfully. The Blender documentation for sound editing in the sequencer only mentions WAV and MP3. However, as you can see in the image here, Blender supports the output of all of those codecs, so I would assume I can play them in the Game Engine as well. If I am wrong in that assumption, feel free to yell at me!
Adding the sound to our game is as easy as adding an actuator. Make sure you are using the Game Logic window layout, select the platform, and go to the Game Logic panel. In there, add a Sound Actuator using the Add Actuator dropdown.
Since we want the sound to run while the platform is rising and falling, we already have the sensors and controllers we need. Simply connect the And controller to the new Sound Actuator.
Note: Of all the objects, you cannot use speakers in the Game Engine. This bug report seems to indicate that it is intentional, but no reasoning is given. That said, not having a speaker object shouldn't limit what you can do.
Now that we have our actuator set up, we can add the sound to it. Simply click on the labeled open and browse to the sound you are using. Once your sound is selected, play Play Mode to Play End. This ensures the sound plays until it ends. Play Stop, will cause the sound to stop as soon as you let go of the shift button. Or, in more general terms, as soon as the connected sensors are no longer active.
If you go into camera view (Numpad 0) and start your game (P), you should be able to walk over to the platform and activate the lift and sound with the use of the shift key.
Note: You may also notice that the sound magically matches the duration of the animation! Well, I'm sorry to say Blender isn't that magical. I set the starter file up that way. If you are using your own animations, you will need to alter the animation or the sound itself so they match up nicely. I simply had to make the animation longer.
Did you catch the bug? If you stand on the platform press the shift button while it is rising or falling, the sound will restart. This is definitely not something we want.
To fix it, add an Actuator sensor in the sensors column. This detects if an actuator is active. Once created, connect the new sensor to the existing And controller.
Now, in the Actuator field, select the Action actuator that controls the animation of the platform. Now click on the Invert setting. This forces it to be True when the platform is not active.
This setting basically tells Blender that, in order for the connected actuators to activate, the Platform animation needs to NOT be active. Since the Actuator sensor by default would require the selected actuator to be active, we select Invert to do the opposite.
Using these same concepts, you should be able to add sound to just about anything. Go make some noise! Also, if you want more information on the sound actuator, you can find some recommended links below. There are a bunch of settings I didn't go over (the 3D sound settings) that you may be interested in.
As always, comments and suggestions are appreciated! I always strive to keep all my tutorials current and up to date!
No single tutorial will cover everything, so if you want to dig deeper, here are some resources for you to check out. If you have other resources you think I should add to this list, feel free to let me know!