In the previous chapter, we learned about the game UI and how to create and add a user interface (also known as a widget) to the screen.
In this chapter, we will learn how to add audio and particle effects to our game. Both of these aspects will increase the quality of our game and produce a much more immersive experience for the player.
Sound in video games can come in the form of either sound effects (also known as SFX) or music. Sound effects make the world around you more believable and alive, while the music helps set the tone for your game. Both these aspects are very important to your game.
In competitive games such as Counter-Strike: Global Offensive (CS: GO), sound is also extremely important because players need to hear the sounds around them, such as gunshots and footsteps, and which direction they came from, to gather as much information about their surroundings as possible.
In this chapter, we will cover the following topics:
Particle effects are important for the same reason that sound effects are important: they make your game world more believable and immersive.
Let’s start this chapter by learning how audio works in UE5.
The project for this chapter can be found in the Chapter09 folder of the code bundle for this book, which can be downloaded here: https://github.com/PacktPublishing/Elevating-Game-Experiences-with-Unreal-Engine-5-Second-Edition.
One of the essential components of any game is sound. Sounds make your game more believable and immersive, which will provide a much better experience for your player. Video games usually have two types of sounds:
2D sounds don’t have any consideration for the listener’s distance and direction, while 3D sounds can be higher or lower in volume and pan to the right or left, depending on the player’s location. 2D sounds are usually used for music, while 3D sounds are usually used for sound effects. The main sound file types are .wav and .mp3.
Here are some of the assets and classes related to audio in UE5:
Note
UE5 has a new audio system called Meta Sounds, which allows developers to create sounds using Digital Signal Processing (DSP). Because this topic is outside the scope of this book, we will not be covering this system. If you want to know more about the system and how it works, you can do so by going to
https://docs.unrealengine.com/5.0/en-US/AudioFeatures/MetaSounds/.
In UE5, we can import existing sounds the same way we would any other asset: either by dragging a file from Windows File Explorer into the Content Browser area or by clicking the Import button in the Content Browser area. We’ll do this in the next exercise.
In this exercise, you will import an existing sound file from your computer into UE5. This audio file will be played when the dodgeball bounces off a surface.
Note
If you don’t have an audio file (either a .mp3 or .wav file) available to complete this exercise, you can download the .mp3 or .wav file available at https://www.freesoundeffects.com/free-track/bounce-1-468901/.
Save this file as BOUNCE.wav.
Once you have an audio file, follow these steps:
Figure 9.1 – The Audio folder in the Content Browser area
Figure 9.2 – The imported audio file
Figure 9.3 – The Sound asset’s settings
The following properties are available in the Sound category:
The only property we’ll be changing is the Class property. We could use one of the existing Sound classes that comes with UE5, but let’s create our own for the Dodgeball game to create a new group of sounds for our game.
Figure 9.4 – Changing the Class property to the Dodgeball Sound Class
Now that this imported sound asset belongs to a specific class, you can group other sound effects related to the dodgeball in the same Sound Class and edit their properties through that Sound Class, which includes Volume, Pitch, and many others.
And with that, we can conclude our exercise. You have learned how to import sounds into your project and how to change their basic properties. Now, let’s move on to the next exercise, where we’ll be playing a sound whenever a dodgeball bounces off a surface in our game.
In this exercise, we will add the necessary functionality to our DodgeballProjectile class so that a sound will play when the dodgeball bounces off a surface.
To do this, follow these steps:
// The sound the dodgeball will make when it bounces off of a
surface
UPROPERTY(EditAnywhere, Category = Sound)
class USoundBase* BounceSound;
#include "Kismet/GameplayStatics.h"
As we saw in Chapter 6, Setting Up Collision Objects, the NormalImpulse property indicates the direction and magnitude of the force that will change the dodgeball’s trajectory after it has been hit. The reason why we want to check whether its magnitude is greater than a certain amount is that when the dodgeball starts losing momentum and bounces off the floor several times per second, we don’t want to play BounceSound several times per second; otherwise, it will generate a lot of noise. So, we will check whether the impulse that the dodgeball is suffering is greater than that amount to make sure this doesn’t happen. If both these things are true, we’ll call the GameplayStatics object’s PlaySoundAtLocation. This function is responsible for playing 3D sounds. It receives five parameters:
Have a look at the following code snippet:
if (BounceSound != nullptr && NormalImpulse.Size() > 600.0f)
{
UGameplayStatics::PlaySoundAtLocation(this, BounceSound,
GetActorLocation(), 1.0f, FMath::RandRange(0.7f, 1.3f));
}
Note
The function responsible for playing 2D sounds is also available from the GameplayStatics object, and it’s called PlaySound2D. This function will receive the same parameters as the PlaySoundAtLocation function, except for the third parameter, which is the origin of the sound.
Figure 9.5 – Setting the BounceSound property to our imported sound
Figure 9.6 – The player character causing the enemy character to throw dodgeballs
If this happens, congratulations – you’ve successfully played a sound using UE5! If you can’t hear the sound playing, make sure that it is audible (it has a level of volume that you can hear).
However, another thing you’ll probably notice is that the sound is always played at the same volume, regardless of the distance that the character is from the dodgeball that is bouncing; the sound isn’t playing in 3D but rather in 2D. To play a sound in 3D using UE5, we’ll have to learn about Sound Attenuation assets.
For a sound to be played in 3D inside UE5, you’ll have to create a Sound Attenuation asset, as we mentioned previously. A Sound Attenuation asset will let you specify how you want a specific sound to change volume as its distance from the listener increases. Have a look at the following example.
Open Unreal Editor, go to the Audio folder inside the Content Browser area, right-click, go to the Sounds category, and select Sound Attenuation. Name this new asset BounceAttenuation:
Figure 9.7 – Creating the Sound Attenuation asset
Open this BounceAttenuation asset.
Sound Attenuation assets have many settings; however, we’ll want to focus mainly on a couple of settings from the Attenuation Distance section:
Figure 9.8 – The Sound Attenuation asset settings
Think of this as two circles around the player, with the smaller circle being the inner circle (with a radius value of Inner Radius) and the bigger circle being the falloff circle (with a radius value of Falloff Distance). If a sound originates from inside the inner circle, it is played at full volume, while a sound that originates from outside the falloff circle is not played at all.
Note
You can find more information on Sound Attenuation assets here: https://docs.unrealengine.com/en-US/Engine/Audio/DistanceModelAttenuation.
Now that you know about Sound Attenuation assets, let’s move on to the next exercise, where we’ll turn the sound that plays when the dodgeball bounces off the ground into a 3D sound.
In this exercise, we’ll be turning the sound that plays when a dodgeball bounces off the ground, which we added in the previous exercise, into a 3D sound. This means that when the dodgeball bounces off a surface, the sound it plays will vary in volume, depending on its distance from the player. We’re doing this so that when the dodgeball is far away, the sound volume will be low, and when it’s close, its volume will be high.
To use the BounceAttenuation asset we created in the previous section, follow these steps:
// The sound attenuation of the previous sound
UPROPERTY(EditAnywhere, Category = Sound)
class USoundAttenuation* BounceSoundAttenuation;
UGameplayStatics::PlaySoundAtLocation(this, BounceSound,
GetActorLocation(), 1.0f, 1.0f, 0.0f,
BounceSoundAttenuation);
Note
Although we only want to pass the additional SoundAttenuation parameter, we must pass all the other parameters that come before it as well.
Figure 9.9 – Setting the BounceSoundAttenuation property to the BounceAttenuation asset
Figure 9.10 – The player character causing the enemy character to throw dodgeballs
With that, we can conclude this exercise. You now know how to play 3D sounds using UE5. We’ll add background music to our game in the next exercise.
In this exercise, we will add game background music to our game. We will do this by creating a new Actor with an Audio component, which, as we mentioned earlier, is appropriate for playing background music. To achieve this, follow these steps:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
class UAudioComponent* AudioComponent;
#include "Components/AudioComponent.h"
PrimaryActorTick.bCanEverTick = false;
AudioComponent =
CreateDefaultSubobject<UAudioComponent>(TEXT("Music
Component"));
Figure 9.11 – The Sound property being updated
Note
Audio components will automatically loop whatever sound they’re playing, so there’s no need to change that Sound asset’s Looping property.
With that, we’ve completed this exercise. You now know how to add simple background music to your game.
Now, let’s jump into the next topic, which is Particle Systems.
Let’s talk about another very important element of many video games: Particle Systems.
In video game terms, a particle is essentially a position in a 3D space that can be represented with an image. A Particle System is a collection of many particles, potentially with different images, shapes, colors, and sizes. The following figure shows an example of two Particle Systems made in UE5:
Figure 9.12 – Two different Particle Systems in UE5
The Particle System on the left is supposed to be electrical sparks that could come from a cable that has been sliced and is now in short-circuit, while the one on the right is supposed to be a fire. Although the particle system on the left is relatively simple, you can tell that the one on the right has more than one type of particle inside it, which can be combined in the same system.
Note
UE5 has two different tools for creating Particle Systems: Cascade and Niagara. Cascade has been present since the beginning of UE5, while Niagara is a system that is more recent and sophisticated and has only been production-ready since May 2020, as of Unreal Engine version 4.25.
Creating Particle Systems in UE5 is outside the scope of this book, but it is recommended that you use Niagara over Cascade, given that it is a more recent addition to the engine.
In this chapter, we will only be using Particle Systems that are already included in UE5, but if you want to create your own, these links will give you more information about both Cascade and Niagara:
Cascade:
https://docs.unrealengine.com/en-US/Engine/Rendering/ParticleSystems/Cascade
https://www.youtube.com/playlist?list=PLZlv_N0_O1gYDLyB3LVfjYIcbBe8NqR8t
Niagara:
https://docs.unrealengine.com/en-US/Engine/Niagara/EmitterEditorReference/index.html
https://docs.unrealengine.com/en-US/Engine/Niagara/QuickStart
We’ll learn how to add Particle Systems to our game in the next exercise. In this chapter, we will simply be using existing Particle Systems that were already made by the UE5 team.
In this exercise, we will learn how to spawn a Particle System in UE5. In this case, we will be spawning an explosion Particle System when a dodgeball thrown by the enemy hits the player.
To achieve this, follow these steps:
The UParticleSystem type is the designation for a Particle System in UE5. Be sure to make this a UPROPERTY and give it the EditDefaultsOnly tag so that it can be edited in the Blueprint class:
// The particle system the dodgeball will spawn when it hits
the player
UPROPERTY(EditAnywhere, Category = Particles)
class UParticleSystem* HitParticles;
This function will spawn an actor that will play the Particle System we pass as a parameter. It receives the following parameters:
if (HitParticles != nullptr)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(),
HitParticles, GetActorTransform());
}
Note
Although we won’t be using it in this project, there is another function related to spawning Particle Systems available from the GameplayStatics object, which is the SpawnEmitterAttached function. This function will spawn a Particle System and attach it to an actor, which might be useful if you want to, for instance, make a moving object light on fire so that the Particle System will always remain attached to that object.
Figure 9.13 – Setting the HitParticles property to P_Explosion
Figure 9.14 – The explosion particle system being played when the dodgeball hits the player
And that concludes this exercise. You now know how to play Particle Systems in UE5. Particle Systems add visual flair to your game and make it more visually appealing.
In the next activity, we’ll be consolidating our knowledge of playing audio in UE5 by playing a sound when the dodgeball hits the player.
In this activity, we will be creating the logic responsible for playing a sound every time the player character gets hit by a dodgeball. In a video game, it’s very important to transmit the player’s crucial information in many ways, so in addition to changing the player character’s health bar, we’ll also be playing a sound when the player gets hit so that the player knows that the character is taking damage.
To do this, follow these steps:
Note
If you don’t have a sound file, you can use the one available at https://www.freesoundeffects.com/free-track/punch-426855/.
When you play the level, you should notice that every time the player gets hit by a dodgeball, you will hear the sound you imported being played:
Figure 9.15 – A sound should play when the player character gets hit
And with those steps complete, you have finished this activity and consolidated the use of playing both 2D and 3D sounds in UE5.
Note
The solution for this activity can be found on https://github.com/PacktPublishing/Elevating-Game-Experiences-with-Unreal-Engine-5-Second-Edition/tree/main/Activity%20solutions.
Now, let’s wrap up this chapter by learning a bit about the concept of level design.
Since Chapter 5, Query with Line Traces, which was related to our dodgeball game, we added a few game mechanics and gameplay opportunities, as well as some audio-visual elements, all of which were handled in this chapter. Now that we have all these game elements, we must bring them together into a level that can be played from start to finish by the player. To do that, let’s learn a bit about level design and level blockouts.
Level design is a specific game design discipline that focuses on building levels in a game. The goal of a level designer is to make a level that is fun to play, introduces new gameplay concepts to the player by using the game mechanics built for that game, contains good pacing (a good balance of action-packed and relaxed gameplay sequences), and much more.
To test the structure of a level, level designers must build what is called a level blockout. This is a very simple and boiled-down version of the level that uses most of the elements that the final level will contain, but it is made using only simple shapes and geometry. This is so that it will be easier and less time-consuming to modify the level in case parts of it need to be changed:
Figure 9.16 – An example of a level blockout made in UE5 using BSP Brushes
Note
It should be noted that level design is its own specific game development skill and is worthy of its own book, of which there are quite a few, but diving into this topic is outside the scope of this book.
In the next exercise, we will be building a simple level blockout using the mechanics we built in the last few chapters.
In this exercise, we will be creating a new level blockout that will contain some structure, where the player will start in a certain place in the level and have to go through a series of obstacles to reach the end of the level. We will be using all the mechanics and objects that we built in the last few chapters to make a level that the player will be able to complete.
Although we will be providing you with a solution in this exercise, you are encouraged to let your creativity loose and come up with a solution yourself, given that there is no right or wrong answer in this case.
To start this exercise, follow these steps:
Keep in mind that assets related to lighting and sound should remain untouched.
Figure 9.17 – Before deleting the required objects
Once you have deleted the objects, your floor should look as follows:
Figure 9.18 – After deleting the required objects
Because building a level, even a simple one, is something that takes a lot of steps and instructions, you will simply be shown a few screenshots of a possible level and, again, be encouraged to come up with your own.
Figure 9.19 – The created level – isometric view
The level can be seen in a top-down view too, as follows:
Figure 9.20 – The created level – top-down view with the player character marked with an arrow
Once you’re happy with the result, this means you have finished your dodgeball game and can now ask your friends and family to play it and see what they think. Great job – you are one step closer to mastering the art of game development!
Before we conclude this chapter, here are some suggestions on what you can do next in this dodgeball project:
There is a whole world of possibilities for expanding the scope of this project. You are encouraged to use the skills you’ve learned and do further research to build new features and add more complexity to your game.
You have now completed the dodgeball game project. In this chapter, you learned how to add polish to your game by playing audio and using Particle Systems. You now know how to add 2D and 3D sounds to your game, as well as some of the tools at your disposal regarding that. Now, you can try to add even more sound effects to your game, such as a special sound effect for when an enemy character sees you for the first time (such as in Metal Gear Solid), a footstep sound effect, or a victory sound effect.
You also built a level using all the tools that you have made throughout the last few chapters, thus culminating all the logic we have built in this project.
In the next chapter, we’ll be starting a new project: the SuperSideScroller game. In that project, you’ll be introduced to such topics as power-ups, collectibles, enemy artificial intelligence (AI), character animation, and much more. You will be creating a side-scrolling platform game where you control a character that must complete a level, collect gems, and use power-ups to avoid the enemies. The two most important topics you will learn about are UE5’s behavior trees and Blackboards, which fuel the AI system, and Animation Blueprints, which allow you to manage your character’s animations.
3.147.103.227