Skip to content
DEV505 / Sound ManagerDOCUMENTATION

DEV505 / CODE RECIPE

Play one step on the right surface.

Choose a surface before playback starts, then let the graph choose and trim one variation.

Start with Your first sound if you have not set up a sound scene before. Use a separate test scene with one enabled Sound Manager, generated Sound Keys, and one Audio Listener near the sound object. Add Footstep from 02-Footstep to the manager's Direct events list; these are the supplied .soundgraph assets under Assets/DEV505/SoundManager/Samples.

Save the complete code below as SurfaceFootsteps.cs in Assets/Scripts. Wait for Unity to compile it, create an empty GameObject, and drag the script onto it. The filename must match the class name. Test one recipe component at a time so another demo controller does not compete for the same events.

No Inspector references are needed. This example has no character controller: its test actions stand in for real footfalls.

In Play Mode, wait until the Sound Manager is ready. To run a listed test action, open the component's three-dot menu in the Inspector and choose that action. These are the script's ContextMenu methods. Your own code or a Unity UI Button can call its public methods too. Inspector edits made during Play Mode are temporary.

Choose Preview grass step, Preview stone step, and Preview wood step from the component menu. Each should produce one short step with the appropriate texture. Repeat a surface several times to hear its variations. Stopping Play Mode ends the test.

Your character controller can call PlayStep(groundType, contactPosition) at a footfall. Here groundType follows the sample’s scene enum: Unknown 0, Grass 1, Stone 2, Wood 3. The script maps that to the graph’s Int: Grass 0, Stone 1, Wood 2. Adapt this one mapping if your game’s ground identifiers differ.

With(SoundParameters.Footstep.Surface, surface) supplies an Int before the graph begins. Its selector reads that value when choosing the clip. Playing first and setting Surface afterward can leave the already selected step unchanged. Unknown ground is skipped.

The example reports refused requests and lets accepted steps end naturally. It does not keep a loop handle or detect your character’s contact with the ground. Use your movement logic or an animation event to decide when to call it; do not call it every frame.

The current graph is 2D even though the context carries a contact position. To make footsteps spatial in your own project, configure the event for 3D and check its distance settings. The graph’s trim selection remains the same.

Open Footstep’s surface groups. Each surface uses four independently trimmed Wave Assets. Select an asset, expand Range in the editing area, preview the recording, and choose a section containing one impact and its tail. Start is the beginning in seconds; Length is the duration. The source file does not need to be split into separate audio files. See the trim walkthrough and Unity close-up.

SurfaceFootsteps.cs
using DEV505.SoundManager;
using UnityEngine;
public sealed class SurfaceFootsteps : MonoBehaviour
{
// Call once when a grounded foot lands. These numbers match the demo's ground enum.
public void PlayStep(int groundType, Vector3 contactPosition)
{
if (!Application.isPlaying || !isActiveAndEnabled) return;
int surface = groundType switch { 1 => 0, 2 => 1, 3 => 2, _ => -1 };
if (surface < 0) return; // Unknown ground or a foot in the air makes no sound.
var context = SoundPlayContext.At(contactPosition)
.With(SoundParameters.Footstep.Surface, surface);
var result = SoundBus.Play(Sounds.Footstep, context);
if (!result.Accepted)
Debug.LogWarning($"Footstep could not play: {result.Status}", this);
// No retained loop: the graph ends this step after its short clip finishes.
}
[ContextMenu("Preview grass step")]
public void Grass() => PlayStep(1, transform.position);
[ContextMenu("Preview stone step")]
public void Stone() => PlayStep(2, transform.position);
[ContextMenu("Preview wood step")]
public void Wood() => PlayStep(3, transform.position);
}

Read the demo walkthrough for the graph, parameters, controls, and Unity screenshots. The related API reference explains the calls used here. Return to all twelve recipes to choose another interaction.

This page imports the checked C# file directly. It was compiled against the documented Unity revision; its runtime checks and source hash are recorded in the documentation repository’s validation files.