Space Shooter: Posting Wwise Events Through Script
In the last article we handled playing a sound from one of the built in Wwise components. I’ll dig into how to post events from a script in this article.
There are lots of reasons to post an event from a script. In this instance, I have the player laser and the enemy laser. If I just put an AkEvent component on the laser prefab, it would just post that event every time which is fine, but what if I want to have some difference between the two? I want the player laser to sound 1 way and the enemy lasers to sound another way.
I already have the functionality built in to determine which laser has fired. You can see the article setting this up here:
In Wwise there are some really need options to add some flavor to your audio files. In this example I want to cover pitch and volume randomization. I want the laser to sound ever-so-slightly different each time its fired. To do this, simply navigate to the laser sound you imported. Check out the Sound Property Editor.
There is a little circle next to the Volume and Pitch controls. Double click it, enable it, and then set your values to what ever you feel sounds good.
Create the event, and add it to the SFX sound bank. ANYTHING WITHIN THE SFX WORK UNIT IN THE EVENTS TAB WILL BE AUTOMATICALLY ADDED TO THE SOUNDBANK SINCE WE DRAGGED THE ENTIRE WORK UNIT INTO THE SOUNDBANK LAST TIME.
Now in Unity, all that needs to happen is posting the event. To do this, open up your laser script and create a variable to hold the Event data. the
AK.Wwise.Event type will be what you need. Then set up logic in the start method that determines whether it was an enemy laser or player laser.
paying playerLaser.Post(gameObject); will cause the sound to play on start or when it is instantiated. The same goes for the enemy laser. the
gameObject argument is basically saying to post this event FROM the game object the script is on. More constant sounds will actually be heard traveling across the screen or move around the player in 3D with this argument. I’ll dig into all that later though ;)
Dont forget to assign the event from the Wwise Picker in the inspector of your laser prefab!
Im sure you can see how posting from script is super useful. If we were to randomize the pitch and volume of an audio clip in Unity we would have to get a reference to the audio clip’s volume and pitch and then use
Random.Range() to have it change. Doing that causes a lot of trial and error. What if the values you put in that function are too little or too much? You have to re-open the script, change them, save it, go back into unity, let it compile, etc etc over and over until you get it right.
Wwise makes quick and easy work of randomization and implementation!