Leçon 5

Table des matières

Setting States using the AkState Component

Just like posting Events, you can either set a State from a component or by script. Premade components allow you to easily and quickly set a State while creating a script offers you more control, and so both methods are highly relevant when integrating sound into a game.

For setting States, the Wwise Unity Integration offers a premade AkState script. The AkState is fairly simple to use, and, by the use of the available Trigger On options, you can very quickly set up a State change for most common purposes. In the following steps, you will learn how to set the MagicZone State when the Player enters the MusicRegion_WwizardMagicHouse Trigger in the Village. The State change will apply an effect to the Master Audio Bus, so once the Player is inside the Wwizard's house the entire soundscape will be affected by a Wwise Flanger and changes in pitch.

  1. In the Unity menu, go to Audiokinetic > Certification > 301 > Lesson 5 and select Setting States using the AkState Component.

  2. In the Hierarchy, expand Wwise > Music Zones and select MusicRegion_WwizardMagicHouse.

    In the Wwizard House you'll see a green line marking the inner dimensions of the house. This is the MusicRegion_WwizardMagicHouse Trigger that you will connect an AkState onto.

  3. [Note]

    Should you not see a green line, you might want to make sure the Mesh Collider component in the Inspector is expanded.

  4. In the Inspector, click Add Component, then search for AkState and select it.

    First, let's select the State.

  5. In the AkState Component's State Name property, expand States > MusicStates > MagicZone and select the MZ2_InsideHouse State and double-click it.

    The AkState component will set the specified State once the Trigger On option is called. You will be using a Trigger to detect when the Player is entering the Wwizard House, so you need the AkTriggerEnter condition.


    In the Trigger On property, you can select multiple conditions, so when you have to change to only one other condition, you first need to select Nothing and then the new Trigger On condition.

  6. Set the Trigger On property to AkTriggerEnter only.

    As you used the AkTriggerEnter to specify the Player picking up a Coin in Lesson 1, you likewise need to declare which game object the Trigger should listen for, by adding an AkTriggerEnter script to the MusicRegion_WwizardMagicHouse Trigger and assigning the Player to it.

  7. Click Add Component, search for AkTriggerEnter and select it.

  8. From the Hierarchy, drag the Player game object into the AkTriggerEnter's Trigger Object property.

    On entering the Trigger, the MagicZone State will be set to Inside and the MagicZone effect will be applied to all sounds in the game. While you are now setting the MagicZone State when entering the Trigger, the State should also be set back once you get outside the Trigger. Start by adding another AkState.

  9. In the Inspector, click Add Component, then search for AkState and add it.

    For detecting whether another Collider exits, you can use the AkTriggerExit option.

  10. Set the AkState Trigger On property to AkTriggerExit only.

  11. In the AkState's State Name, expand MagicZone then select the MZ1_OutsideHouse State and double-click it.

    Just like the AkTriggerEnter needs an AkTriggerEnter script just to detect the Player, you should also add the AkTriggerExit script because you are using the corresponding Trigger On option.

  12. Select Add component, search for the AkTriggerExit component and select it.

  13. From the Hierarchy, drag the Player game object into the AkTriggerExit's Trigger Object property.

    Let's try it out and pay attention to how it affects the music, ambiences, and interactive sounds.

  14. Enter Play mode, run to the Wwizard House and enter it.

    Pay attention to how all sounds are altered inside the Wwizard House. You can try to swing the sword a few times to hear that the voice of the adventurer is also pitched up slightly.

  15. Exit the Wwizard House.

    By exiting the house, the soundscape should return to normal without the Flanger effect on the Master Audio Bus and the high pitched Adventurer voice when attacking. As such, the soundscape will dynamically change when the Player enters or exits the Wwizard House. Click Play again to exit Play mode.

That's it! You are now changing the MusicZone State once the Player enters or exits the MusicRegion_WwizardMagicHouse. As such, the AkState component allows you to very quickly change a State and, combined with the AkTriggerEnter and AkTriggerExit script, you can specify exactly which game object you want it to react to.


To set Switches in Unity, you can use the same procedure with the AkSwitch script.

Cette page a-t-elle été utile ?