# Playing different footstep noises depending on surfaces. (2D)

**URL:** <https://qa.fmod.com/t/playing-different-footstep-noises-depending-on-surfaces-2d/20629>\
**Category:** Unity\
**Tags:** unity, csharp\
**Created:** [September 7, 2023, 1:12pm UTC](https://qa.fmod.com/t/playing-different-footstep-noises-depending-on-surfaces-2d/20629 "2023-09-07T13:12:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BardstownDev](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@BardstownDev](https://qa.fmod.com/u/BardstownDev)\
**Post date:** [September 7, 2023, 1:12pm UTC](https://qa.fmod.com/t/playing-different-footstep-noises-depending-on-surfaces-2d/20629/1 "2023-09-07T13:12:20Z")

</div>

Hi there, I’ve been trying to get FMOD to play different sound clips depending on the character’s surface they are standing on. Right now, I’ve decided to use a Scriptable Object (SO) to handle this.

In theory, the collider that contains a tag that displays which material the player is standing on has a SO that contains this code:

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;  
using FMOD.Studio;  
using FMODUnity;

[CreateAssetMenu(fileName = “Surfaces”, menuName = “ScriptableObjects/SurfaceScriptableObject”, order = 1)]  
public class SurfaceScriptables : ScriptableObject  
{  
EventInstance playerWalkingOnConcreteFlat;  
EventInstance playerWalkingOnWoodFlat;

```
void Start()
{
    playerWalkingOnConcreteFlat = AudioManager.instance.CreateEventInstance(FModEvents.instance.PlayerWalkingOnConcreteFlat);
    playerWalkingOnWoodFlat = AudioManager.instance.CreateEventInstance(FModEvents.instance.PlayerWalkingOnWoodFlat);

}

void OnTriggerStay2D(Collider2D col)
{

    if (col.tag == "Player")
    {
        {
            aSoundInstance = RuntimeManager.CreateInstance(aSound);
            aSoundInstance.start();
        }
    }

}

        void OnTriggerExit2D(Collider2D col)
    {
        aSoundInstance.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);

    }

```

}

(Doesn’t compile yet)

And this code is supposed to take whatever audio file is inserted into the SO and insert it into the code block, then play the code to loop his walking sound, before terminating after he leaves the material OR stops moving.

So far I have no idea how to execute this. It should be pretty simple in theory.

1. SO checks when the player collides with it.
2. Each SO for the corresponding material has an audio file containing the walk and run sounds.
3. When it detects a collision and the player moving faster than 0.1 speed, it should play the footstep sound, and stop when the player moves slower than 0.1, OR leaves the collider.

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [September 8, 2023, 6:41am UTC](https://qa.fmod.com/t/playing-different-footstep-noises-depending-on-surfaces-2d/20629/2 "2023-09-08T06:41:15Z")

</div>

Hi,

What version of FMOD and Unity are you using?

Just to confirm:

1. `AudioManager` and `FModEvents` are singletons that you have created?
2. You are creating the `EventInstances` `playerWalkingOnXX` then in the `OnTriggers` you are using `aSoundInstance`.
3. If you substitute `aSoundInstance` for one of the others you are creating does this allow you to hear sounds?

```auto
void OnTriggerStay2D(Collider2D col)
{
	if (col.tag == "Player")
	{
		{
			playerWalkingOnConcreteFlat.start();
		}
	}
}
void OnTriggerExit2D(Collider2D col)
{
	playerWalkingOnConcreteFlat.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
}

```

Most FMOD functions return an [FMOD Engine | Core API Reference - FMOD\_RESULT](https://fmod.com/docs/2.02/api/core-api-common.html#fmod_result). I would suggest adding these to your FMOD calls and logging the result if they do not resolve `FMOD_OK`. This will hopefully provide more information where the function may be failing.

```auto
FMOD.RESULT result = playerWalkingOnConcreteFlat.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
if (result != FMOD.RESULT.OK)
{
	Debug.Log(result);
}

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![BardstownDev](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@BardstownDev](https://qa.fmod.com/u/BardstownDev)\
**Post date:** [September 8, 2023, 7:03pm UTC](https://qa.fmod.com/t/playing-different-footstep-noises-depending-on-surfaces-2d/20629/3 "2023-09-08T19:03:06Z")

</div>

1. I am using version 2021.3.16 and fmod version 2.02.15.
2. Yes.
3. Not sure I follow.

Here is an updated version including usage of a Scriptable Object (SO) to store any Eventinstances related to Fmod events:

Compiles fine!

FootstepSounds script: [using System.Collections;using System.Collections.Generic;using UnityEngine; - Pastebin.com](https://pastebin.com/4tqSkchg)

SO script: [using System.Collections;using System.Collections.Generic;using UnityEngine; - Pastebin.com](https://pastebin.com/0JMwwmiX)

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [September 11, 2023, 1:47am UTC](https://qa.fmod.com/t/playing-different-footstep-noises-depending-on-surfaces-2d/20629/4 "2023-09-11T01:47:01Z")

</div>

Hi,

That is good to hear!

Another test you could do is checking the created event instance `isValid()`, [FMOD Engine | Studio API Reference - EventInstance::isValid()](https://fmod.com/docs/2.02/api/studio-api-eventinstance.html#studio_eventinstance_isvalid).

```auto
FootstepScriptables.playerWalkingOnConcreteFlat = AudioManager.instance.CreateEventInstance(FModEvents.instance.PlayerWalkingOnConcreteFlat);

if (!FootstepScriptables.playerWalkingOnConcreteFlat.isValid())
{
    // Log error here
}

```

What are the current issues you are running into now that it is compiling?
