Book Image

Unity Game Development Scripting

By : Kyle D'Aoust
Book Image

Unity Game Development Scripting

By: Kyle D'Aoust

Overview of this book

Table of Contents (17 chapters)
Unity Game Development Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Sound effects


To play sound effects within our game, we will make an event-based system so that playing the sound effects will be very easy. Sound effects are what bring your items, events, and characters to life! They give your gun the loud bang you would expect, the grunt from your character as they climb a wall, and the notification sound that you get when you hover over an option in the menu. Sound effects add to the immersion of your game and playing them can be very easy.

Creating the script and variables

Our first step in playing sound effects will be to create the C# script and name it SFX_Manager. Next, we will need a few variables; add these to your script:

public float sfxVolume = 1.00f;
public AudioClip Run, Spell, Strike;
public GameObject RunSource;

The first variable should look familiar by now. We will use it for the volume. Next, we create a few audio clips. For this chapter, we will use three specific sound effects for testing purposes. The last variable is a GameObject that...