Switches are a type of Game Sync that allow us to define variations for a specific sound object. They are used to implement multi-surface footsteps sounds for example, by switching to the correct set of sounds depending on the surface the player is walking on. In contrast to States, Switches are applied on a local game object scope, that means that the Switch is applied on a particular game object and can exist for many instances of individual game objects. In this tutorial we will create switches in the Wwise authoring tool and learn to set them in Unity by using the components provided by the Wwise integration plugin and by writing C# code.
Creating Switches in the Wwise Authoring Tool
Start by right clicking on a Work Unit in the Actor-Mixer Hierarchy and selecting a Switch Container:
After that you can insert individual sound objects inside the Switch Container. In this example I added several random containers with footsteps sounds in different terrain settings:
Switch to Game Syncs Tab and add a Switch Group to your desired Work Unity. For each Switch, add a child Switch to the Switch Group:
Go back to the Switch Container and select the Switch Group you created in the General Settings in the Group field:
Make sure to also define a Default Switch to avoid any errors in the game engine.
Now you can assign the Sound Objects inside your Switch Container to the Switches in the Switch Group by dragging and dropping the objects from the Contents Editor to the Switches (Assigned Objects) on the right:
Finally create a new Event and assign the Switch Container created in the first place as a target of the play action:
As a last step add the Event to your desired SoundBank (don’t forget to generate the banks!).
Setting a Wwise Switch using the Ak Switch component in Unity
Supposing we want to set a Switch when entering a trigger collider, we Insert the Ak Switch component into your GameObject containing the collider:
The AKSwitch component has following properties:
- Trigger On: A list of Unity Events that will trigger the Switch.
- Use Other Object: Defines if the Switch applies to the current or another GameObject. Make sure to check this setting and use the appropriate component provided by the Wwise integration if you want another GameObject to trigger the Switch. In the example above we want to trigger the Switch when the player enters the trigger collider. You would want to place the Ak Trigger Enter component in your collider’s GameObject and drag the player GameObject into the Trigger Object field.
- Name: The name of the Switch the will be triggered.
Since we want the player to trigger the Switch we add the Ak Trigger Enter component to the GameObject containing Ak Switch and attach the player GameObject to the Trigger Object Field:
Make sure to have the option Use Other Object selected and now the Switch should successfully be set when entering the box trigger collider.
Setting a Wwise Switch using AkSoundEngine.SetSwitch in Unity
We can use the
SetSwitch method of the AkSoundEngine namespace to directly set a Switch in code. The code looks like this:
AkSoundEngine.SetSwitch(string switchGroup, string switchState, GameObject gameObject);
switchGroup is the Switch Group created in the Wwise authoring tool. switchState the Switch we want to set. gameObject refers to the GameObject in which we post the Event playing the Switch Container.
Setting a Wwise Switch using the AK.Wwise.Switch Type in Unity
Alternatively, we can use the Wwise.Switch Type to easily set the Switch in code. To do this, declare the Switch Type property first:
[SerializeField] private AK.Wwise.Switch mySwitch;
You can now select the appropriate Switch from the inspector window in Unity:
To set the Switch value simply call the
SetValue method of the Wwise.Switch Type:
Make sure to pass the GameObject that will post the Event as an argument in the method. That’s it!