By November 8, 2018Uncategorized

Our first sound is in! As it is a 2D ambience, the sound plays in the background from when we enter play mode. What about sounds that are triggered as we move around?

  1. First, create a new GameObject in the Unity hierarchy by right-clicking in the hierarchy and pressing ‘create empty’. Call it whatever you like, I’ll go for ‘Door’.
  2. In the inspector, hit ‘add component’ and add a box collider using the search field. Once we have added the box collider, tick the ‘Is Trigger’ checkbox. This collider can now be used to trigger events.
  3. Next, we need to Tag the FPSController object, or your own equivalent. Select the FPSController GameObject in the hierarchy, and at the top of the inspector select the ‘Tag’ drop-down menu. Tag the FPSController as ‘Player’. This is very important, as we will shortly see.
  4. Go to our Audio_Scripts folder, and create a new C# script called Trigger_Enter. Copy the following code.
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Trigger_Enter : MonoBehaviour
        //wwise event
        public string wwiseEvent;
        //GameObject emitting sound
        public GameObject location;
        //Wwise Event will be called when a GameObject with the "Player" tag
        //enters the collider of the GameObject this component is attached to
        private void OnTriggerEnter(Collider other)
            if (other.gameObject.tag == "Player")
                AkSoundEngine.PostEvent(wwiseEvent, location);
  5. This code is telling Unity to call the Wwise Event only when a collision happens with the GameObject, and only if the colliding GameObject has the “Player” tag. Without the “Player” tag, any colliding object would call our Wwise event! With the Player tag, the sound will only be triggered when the player collides with the GameObject. Add the Trigger_Enter script as a component to the ‘Door’ GameObject (drag and drop, or select Add Component in the inspector).
    Note that I have used the same variable names for this example, ‘wwiseEvent’ and ‘location’, as the last example. This is generally bad practise, particularly once you begin writing scripts and classes that reference each other within the game. Each variable should have a unique name to avoid confusion down the line, but this will do for these standalone examples.
  6. Now, we must create a new Wwise event. Use the same process as before, adding a sound into Wwise and creating a Play event. Add the event to the SoundBank, save the Wwise session and Generate SoundBanks.
  7. Go back to the ‘Door’ GameObject in Unity, and in the Hierarchy we will see the Trigger_Enter script with the wwiseEvent and location fields. Add the Wwise Event name, and as the location add the ‘Door’ GameObject, or your equivalent. This means that the sound will emit from the ‘Door’ GameObject in Unity.
  8. Enter play mode in Unity, and move to where the GameObject is positioned. We should hear the event is triggered when we reach the collider.
  9. Try copying this script into a new C# script titled Trigger_Exit, and replacing ‘OnTriggerEnter’ with ‘OnTriggerExit’. Note that the sound will now be triggered when the FPSController leaves the collider, not when it enters.

    TroubleshootingWwise Event ID Not Found – Similar to the previous post, SoundBanks have not been generated in Wwise, or have not been loaded in Unity. The event name in Wwise may not match the event name in the wwiseEvent field in the inspector in Unity.

    Box Collider not triggering sound – ensure the ‘Is Trigger’ checkbox is selected in the inspector in Unity, where we added the Box Collider component. If it is not a trigger, it will not act as a trigger during collisions.

    #wwise #unity #trigger #collider #ontriggerenter

Leave a Reply