Leçon 5

Table des matières

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 sounds 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 sound.

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 > Certification > 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 type, 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_Type 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 and 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 surface 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.

  9. 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 check the 'Selection Outline' box in the scene view under the Gizmos tab

  10. From the Inspector, open the custom SoundMaterial script, 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 a 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 surface is hit. In WAG, all SoundMaterial scripts have already have been assigned to it's appropriate Switch (in this case, dirt) by the designer creating the environment model. You will not be editing the script, but you should be aware that this is where the Wwise-Type Switch property drawer is located, which you will be referencing from the PostWwiseEvent script, is located.

    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.

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

  12. In the Inspector, open the PostWwiseEvent script.

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

  13. 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 () {
        
        }
    }

  14. 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 type (material) beneath the Player's feet, a custom script called MaterialChecker has been added to the each of the Player's two 'toe' game objects. You don't need to understand it in detail, but suffice it to say that the script is able to look below the Player's left or right foot, find the surface game object, look up the SoundMaterial attached to it and retrieve the material property's Switch value.

    But how do we find the MaterialChecker script? To easily manage the Player's stats, related game object, and so on, a custom PlayerManager script has been created, where you will find each of the Player's feet. Let's start by writing the path to the MaterialChecker's GetMaterial() function.

  15. 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);
    }

    You now have the GetMaterial() function, which will look beneath each of the Player's feet (foot_R and foot_L) and return the SoundMaterial's current Switch value. Next, you can use the .SetValue() function to define a Switch before the Event is posted.

    [Tip]

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

  16. 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.

    [Note]

    In Unity, you get the game object that the script is added to, by typing 'gameObject'.

  17. 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. 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 left 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. And again, remember that you should always save the changes in the script, so that they are applied in Unity.

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

    Next, repeat the test procedure.

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

  20. 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. If you want to learn how to set up such a system, take a look at the AdventuressAnimationEventHandler script's TakeFootstep() function.


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