Footsteps

By November 9, 2018Uncategorized

Now we have our ambience, and hopefully a few spot sounds around the scene. If using the FPSController, Unity’s default footstep sounds are applied, but they are repetitive and we will always want to use our own samples, changing them according to the surface our character is on. This involves a lot of prep work in Wwise and Unity, but it is a very important part of building a game. There are a number of ways to implement footsteps. If using the FPSController in Unity, we can replace their footstep sound with ours in the FPSController script. If your game has character animation, we can call our footstep event during the animation sequence, when the characters foot touches the grand.

  1. Go to the ‘Game Syncs’ tab in Wwise. Right-click the Default Work Unit under the Switches heading, and create a new Switch group called Footsteps. Now, right-click the Footsteps Switch group, and create two new switches – Grass and Concrete (or whatever footstep sounds you are using). More info on working with Switches here.
  2. Now, we need to set up our Footsteps event in Wwise. Go to the Actor-Mixer hierarchy, and right-click on the Default Work Unit. Create a new Switch container, also Footsteps. In the property editor here, we can see the ‘Group’ and ‘Default’ options. We can select our ‘Footsteps’ Switch group here, and choose which surface we want to be our default footstep sound.
  3. Right-click on the Footsteps Switch container, and add two random containers as children. These will contain our footsteps. Add grass footsteps to one random container, and concrete to the other.
  4. With the Footsteps Switch container selected, we can see under the property editor there is a tab for ‘assigned objects’, with our Switches. Drag and drop the Grass Random container to the Grass Switch, and the Concrete Random container to the Concrete switch.
  5. Once this is done, right-click the ‘Footsteps’ Switch Container and create a Play event, adding it to the SoundBank like any other. We can test to see if our Switch container is working by changing the Switch in the transport control.
  6. Now, move over to Unity. Presuming we have downloaded the ‘Characters’ assets package, and are using the FPSController, we will need to move the FirstPersonController script into our Audio_Scripts folder. This is purely hierarchical, as Wwise only looks in top level folders. The FirstPersonController script can be found here, simply drag the script into our Audio_Scripts folder.
  7. Open the FirstPersonController script (it will look quite daunting). Navigate to line 159 of the code, which should look like this.
    PlayFootStepAudio();
    
  8. Highlight this line of code, replace it with
    AkSoundEngine.PostEvent("Play_Footsteps", gameObject);
    

    and save the script.

  9. Enter Play Mode in Unity, and now as we move around we should hear our Default footsteps! Now, how do we switch to our second set of footsteps?
  10. Create a new empty GameObject, and add a Box Collider that is a Trigger.
  11. Create a new C# script in our Audio_Scripts folder, name it Footsteps_Switch. Copy the following code in.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Footsteps_Switch : MonoBehaviour {

    public string switchGroup;
    public string switchEnter;
    public string switchExit;
    public GameObject location;

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            AkSoundEngine.SetSwitch(switchGroup, switchEnter, location);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            AkSoundEngine.SetSwitch(switchGroup, switchExit, location);
        }
    }
}
//https://audioprogblog.com/

Now drag the Footsteps_Switch script onto the GameObject, and in the inspector we will see fields for the Switch Group, the switch we want to use when we enter the collider, and the switch we want to use when we exit the collider, as they are named in Wwise. Enter the names into these fields, and use the FPSController as the location GameObject.

Enter Play mode again. Presuming everything is working correctly, you should be moving into and out of the collider and hearing the footsteps change. But there is one more issue here – try jumping! We will hear that the footsteps continue playing even if our character is in the air. Thankfully, in the FirstPersonController script, there already exists a variable that tells us when the player is grounded.

Open the FirstPersonController script, and navigate back to line 159, where we previously entered our Wwise Event. We will add an If Statement that will only play the Wwise Event IF the character is grounded.

if(m_CharacterController.isGrounded)
{
AkSoundEngine.PostEvent("Play_Footsteps", gameObject);
}

Troubleshooting

Error posting Wwise Event – make sure you have dragged the FPSController script into the Audio_Scripts folder, and that the Audio_Scripts folder is a top level folder, directly under the assets folder.

Ensure that the Switch Group, and Switch Enter/Exit fields are spelled correctly (case sensitive)

Event not triggering but no error – Ensure the FPSController or equivalent is set to the location. Ensure the FPSController is tagged as “Player”.

Compile error in FirstPersonController script – It’s possible that through making changes to the code, you may forget a semicolon, remove a curly bracket or forget parentheses. Read through the examples and make sure you remove, copy and paste the code exactly as it is formatted here.

#wwise #unity #footsteps #firstpersoncontroller #FPSController #jump #switch

Next Post

Leave a Reply