segunda-feira, 15 de junho de 2015

Playing Sounds with Blueprints

Sound effects and music must be in the ".WAV" format in order to be imported into the Unreal Engine 4. To import, simply click the "Import" button on the Content Browser and choose the ".WAV" file. After importing, it creates an element of the "Sound Wave" type which can already be used in the game.

To create complex sounds that can be dynamically modified during the game you must create a "Sound Cue". There is the "Sound Cue" editor that has many actions that allow you to combine several "Sound Wave" into a single "Sound Cue" with interesting effects.

There is a simple way to play a sound in Blueprints. Just use the action "Play Sound at Location":

In this action choose in the combobox the sound to play. This sound can be a "Sound Wave" or a "Sound Cue". In parameter "Location" can be used the position of the Actor related to the sound. Imagine that the above example is a Blueprint of some type of explosive. When the player touches the explosive, the sound is played in the location where the Blueprint is.

For background music or sounds of the level, we can use an actor of the type "AmbientSound". As an example we will create a background music using the Sound Wave "Starter_Music01" from the Starter Content, which is in the "StarterContent/Audio" folder:

Click to enlarge

Drag the Sound Wave "Starter_Music01" and drop anywhere in the Level. The editor will automatically create an actor of the type "AmbientSound":

On the "Details" tab you can change the sound that is being used and set whether to use "Spatialization" and "Attenuation". "Spatialization" positions the sound in 3D space. "Attenuation" defines how the sound decreases or stops, as the player moves away from the position of the sound source. For background music is not necessary "Spatialization" neither "Attenuation".

The best way to have control over sound and music is using "Audio Component" within the Blueprints. As an example, imagine that we have two songs. One is the music that plays during the game and the other is a victory music that plays when the goal of the game is reached.

To add a song click on the "Add Component" button of a Blueprint and choose "Audio". Create two "Audio Component" with the name "GameMusic" and "VictoryMusic". For each of them, in the "Details" tab, select in the combobox "sound" the song that will be used and uncheck the property "AutoActivate" so that the song does not start playing automatically.

A Custom Event was created with the name "Victory" that must be called when the goal of the game is reached. The image below shows the transition between the songs:

Click to enlarge

The "GameMusic" starts at the beginning of the game. When the event "Victory" is triggered we use the action "Fade Out" to gradually decrease the volume of "GameMusic" until stop in 2 seconds. The "Fade In" action gradually increases the volume of the "VictoryMusic" until it reaches its normal volume in 2 seconds.