第 5 课

目录

Setting a Switch using a Wwise-Type

[Note]

Before engaging this section, you must complete Lesson 4 - Posting Events from Script.

While you are successfully posting the Adventurer's footsteps when her foot is touching the ground, you are not yet accounting for surface type variations.

Right now, every footstep sound similar, as they are all using random footstep sounds from the same surface material, dirt (the default surface material). To make the footsteps sound more natural, you can change what sounds are played according to the type of surface beneath the Player's feet. In Lesson 4, you wrote a script, PostWwiseEvent, that posts an Event each time one of the Adventurer's feet is touching the ground in the Player_Sprint Animation clip. In this section you will add some functionality to that script to account for the different surfaces in the game. A simple way to ensure that the footstep sound is using the correct surface material is to set the type of surface material before posting each footstep Event.

When posting the Player_Footstep Event, the Event call will be navigated through two Switches and a Random Container to select and play a single footstep audio file.

First, the type of surface will be determined using the Footstep_Surface Switch Container and, secondly, the Players movement speed will determine whether a walking or running footstep will be played. The Surface_Type Switch Group is assigned to the Footstep_Surface Switch Container. As such, by setting this Switch before the Player_Footstep Event is posted, the Footstep_Surface Switch Container will be ready to direct the call to the correct footstep material Switch Container.

In the following steps, you will be asked to set the Surface_Type Switch Group before posting the Player_Footstep Event.

  1. In Unity, go to Audiokinetic > Certifications > 301 > Lesson 5 and select Setting a Switch using a Wwise-Type.

    First off, let's make sure that the PostWwiseEvent script is still attached to the Player game object.

  2. In the Hierarchy, select the Player game object.

    The Player game object is a Prefab, and when you pressed Apply in Lesson 4 - Posting Events from Animations, the changes should have been applied to all instances of the Player game object across all Scenes. Look in the Inspector. If you do not see the PostWwiseEvent script, return to Lesson 4 - Posting Events from Animations and complete all steps. If you see it, you can continue.

    To demonstrate the problem of not detecting surface materials, let's connect using the profiler.

  3. In Wwise, go to Layouts and select Profiler.

  4. Click Remote... and then click Connect, having selected the WAG game in the Editor.

  5. In Unity, click Play.

    Make sure not to attack anything, only move the Adventurer so that the Player's attacks using the same Surface_Material Switch Group will not clutter the Capture Log.

  6. Run from the dirt into grass and out again.

  7. Exit Play mode, and in Wwise's Capture Log, click on the Filter... button.

    There is a lot of information in the Capture Log. To get an overview of Switch changes, we'll filter out anything except Switches.

  8. Disable all Types, except for Switches.

  9. Click OK.

    When you were running from the dirt surface to the grass surface, the Surface_Type should have been set to grass, which would direct the Player_Footsteps call to different sounds. Take a look in the Capture Log at specifically the Description and Wwise Object columns.

    Currently, the Surface_Type Switch is not being set, meaning that the default Switch value (dirt) will continue to be used by the Footstep_Surface Switch Container. To set the Switch, you will first need a bit of understanding of how the material type system works in the Wwise Adventure Game. Afterwards, you will set the Switch before posting the Event in the PostWwiseEvent script you created in Lesson 4.

    For the next step, make sure you have selected the Move tool.

  10. In the Unity Scene view, select the brown Road Space game object below the Adventurer.

    If you are not seeing an outline on the ground, you can turn on the onlines in the Scene view in the Gizmos tab.

  11. From the Inspector, open the custom SoundMaterialscript, developed specifically for the Wwise Adventure Game.

    The SoundMaterial script is a simple and short script, but nevertheless useful. It contains a single Wwise-Type property called material and it's used as way to label a game object directly with a Wwise-Type Switch, so that multiple methods like footsteps or weapon attacks can use this property to know what type of material is hit.

    You will not be editing the script, but you should be aware that this is where the material property, which you will be referencing from the PostWwiseEvent script, is located.

  12. In the Hierarchy, select the Player game object.

  13. In the Inspector, open the PostWwiseEvent script.

    Let's first delete the function description above the PlayFootstepSound() function, as it doesn't apply anymore.

  14. Delete the entire line saying // Use this for initialization

        
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
        
    public class PostWwiseEvent : MonoBehaviour {
        public AK.Wwise.Event MyEvent;
        public void PlayFootstepSound(){
            MyEvent.Post(gameObject);
        }
        
        // Update is called once per frame.
        void Update () {
        
        }
    }

  15. Add one line of space above the "MyEvent.Post(gameObject)" function call.

    public AK.Wwise.Event MyEvent;
    public void PlayFootstepSound(){
        MyEvent.Post(gameObject);
    }

    To detect the surface material beneath the Player's feet, we added the custom MaterialChecker script as a component of the toe game object. This script is able to look below the Player's foot, find the surface game object, and look up the SoundMaterial on it. To get the current material below a foot, you call the GetMaterial() function. This function will simply retrieve the SoundMaterial's Wwise-Type Switch named material, allowing you to set its Switch value.

  16. In the empty space above the MyEvent.Post(gameobject); line, type PlayerManager.foot_L.GetMaterial().

    public void PlayFootstepSound(){
        PlayerManager.foot_L.GetMaterial()
        MyEvent.Post(gameObject);
    }

    To set a Wwise-Type Switch you use the SetValue() function, which will set whatever Switch that was set in the Inspector.

    [Tip]

    You set a State similarly to a Switch by using the .SetValue() function. Learn more about setting States in Lesson 8.

  17. Continued from PlayerManager.foot_L.GetMaterial(), type .SetValue();

    public void PlayFootstepSound(){
        PlayerManager.foot_L.GetMaterial().SetValue();
        MyEvent.Post(gameObject);
    }

    Notice that the SetValue() function is underlined in red because you will need to specify a game object in the SetValue parentheses. Contrary to States, which work on a global scale (there can only be one current State throughout the entire game), Switches work on a per game object basis. This means that when changing the Switch you only affect the posted Events on the same game object. In the current scene, PostWwiseEvent is located on the Player game object. So, to set a new Switch for the Player_Footstep Event, you need to refer to the same game object that the Player_Footstep Event is posted on.

  18. Inside the SetValue() parentheses, type gameObject.

        
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
            
    public class PostWwiseEvent : MonoBehaviour {
        public AK.Wwise.Event MyEvent;
        public void PlayFootstepSound(){
            PlayerManager.foot_L.GetMaterial().SetValue(gameObject);
            MyEvent.Post(gameObject);
        }
    
        // Update is called once per frame.
        void Update () {
        
        }
    }

    Let's summarize up what you just enabled. The Player_Sprint Animation Clip will trigger an Animation Event linked to the PlayFootstepSound() function. This function has two objectives. It will use the CheckMaterial's GetMaterial() function, referenced through the PlayerManager, to retrieve the material below the Players toe. As the material property is a Wwise-Type Switch, you can then use the .SetValue() function to set the Switch (from the surface beneath the toe) on the Player game object. As the Player is now set to the correct Switch, the Player_Footstep Event can now be posted on the same game object. Lastly, you should always save the changes in the script, so that it is applied in Unity.

  19. Press CTRL+S on Windows or CMD+S on Mac to save the script.

    Next, repeat the test procedure.

  20. Remote Connect to Unity, Play the scene, and repeatedly run from one type of surface to another.

  21. Exit Play mode, and in Wwise, Inspect the Capture Log's Description and Wwise Object columns. Notice that both the Dirt and Grass Switches are now being set.

By labeling the surfaces in the game with Wwise-Type Switches, you were able to make the footstep sound change dynamically depending on the surface beneath the Player. As such, once the system is set up, all you would have to maintain when building more regions in the game would be to label the surfaces to what they should sound like.

[Tip]

The current steps show how to get the SoundMaterial below the left foot only, but ideally you should get the SoundMaterial alternately from the left and right foot. To see how the Players footsteps was fully implemented in the Wwise Adventure Game, take a look at the AdventuressAnimationEventHandler script's TakeFootstep() function, which accounts for different type of foot (left and right) or whether the player is in water.