Lesson 3.4 - Particles and Sound Effects

Tutorial

·

Beginner

·

+10XP

·

60 mins

·

(1230)

Unity Technologies

Lesson 3.4 - Particles and Sound Effects

Overview:

This game is looking extremely good, but it’s missing something critical: Sound effects and Particle effects! Sounds and music will breathe life into an otherwise silent game world, and particles will make the player’s actions more dynamic and eye-popping. In this lesson, we will add cool sounds and particles when the character is running, jumping, and crashing.

Project Outcome:

Music will play as the player runs through the scene, kicking up dirt particles in a spray behind their feet. A springy sound will play as they jump and a boom will play as they crash, bursting in a cloud of smoke particles as they fall over.

Languages available:

Overview Video

1. Customize an explosion particle

The first particle effect we should add is an explosion for when the player collides with an obstacle.



  1. From the Course Library > Particles, drag FX_Explosion_Smoke into the hierarchy, then use the Play / Restart / Stop buttons to preview it

  1. Play around with the settings to get your particle system the way you want it

  1. Make sure to uncheck the Play on Awake setting

  1. Drag the particle onto your player to make it a child object, then position it relative to the player

2. Play the particle on collision

We discovered the particle effects and found an explosion for the crash, but we need to assign it to the Player Controller and write some new code in order to play it.



  1. In PlayerController.cs, declare a new public ParticleSystem explosionParticle;

  1. In the Inspector, assign the explosion to the explosion particle variable

  1. In the if-statement where the player collides with an obstacle, call explosionParticle.Play();, then test and tweak the particle properties


3. Add a dirt splatter particle

The next particle effect we need is a dirt splatter, to make it seem like the player is kicking up ground as they sprint through the scene. The trick is that the particle should only play when the player is on the ground.



  1. Drag FX_DirtSplatter as the Player’s child object, reposition it, rotate it, and edit its settings

  1. Declare a new public ParticleSystem dirtParticle;, then assign it in the Inspector

  1. Add dirtParticle.Stop(); when the player jumps or collides with an obstacle

  1. Add dirtParticle.Play(); when the player lands on the ground


4. Add music to the camera object

Our particle effects are looking good, so it’s time to move on to sounds! In order to add music, we need to attach sound component to the camera. After all, the camera is the eyes AND the ears of the scene.



  1. Select the Main Camera object, then Add Component > Audio Source

  1. From Course Library > Sound, drag a music clip onto the Audio Resource variable in the inspector

  1. Reduce the volume so it will be easier to hear sound effects

  1. Check the Loop checkbox

5. Declare variables for Audio Clips

Now that we’ve got some nice music playing, it’s time to add some sound effects. This time audio clips will emanate from the player, rather than the camera itself.



  1. In PlayerController.cs, declare a new public AudioClip jumpSound; and a new public AudioClip crashSound;

  1. From Course Library > Sound, drag a clip onto each new sound variable in the inspector

6. Play Audio Clips on jump and crash

We’ve assigned audio clips to the jump and the crash in PlayerController. Now we need to play them at the right time, giving our game a full audio experience



  1. Add an Audio Source component to the Player

  1. Declare a new private AudioSource playerAudio; and initialize it as playerAudio = GetComponent<AudioSource>();

  1. Call playerAudio.PlayOneShot(jumpSound, 1.0f); when the character jumps

  1. Call playerAudio.PlayOneShot(crashSound, 1.0f); when the character crashes


7. Lesson Recap

New Functionality:


  • Music plays during the game

  • Particle effects at the player’s feet when they run

  • Sound effects and explosion when the player hits an obstacle

New Concepts and Skills:


  • Particle systems

  • Child object positioning

  • Audio clips and Audio sources

  • Play and stop sound effects

Overall Recap:


  • We’ve made an incredibly polished game - we have these super cool sound and particle effects. We have upbeat background music. We learned how to utilize animations for our characters, and we did some programming magic to make our background endlessly scroll.

Complete this tutorial