Different terrains script UNITY. Parameter doesn't change

Hi, I’m having issues with my attempt to implement footsteps for different terrains using Unity and FMOD. The footsteps are triggered by animation events, but I can hear only one kind of terrain when playing. I guess there’s something wrong with the script that is not changing the parameters in FMOD.

I’m using a script by Alessando Fama footsteps guide (alessandrofama.com/tutorials/fmod-unity/footsteps/unity/footsteps/). Here it is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFootsteps : MonoBehaviour {
private enum CURRENT_TERRAIN { GRASS, GRAVEL, WOOD_FLOOR, WATER };
[SerializeField]
private CURRENT_TERRAIN currentTerrain;
private FMOD.Studio.EventInstance foosteps;
private void Update()
{
DetermineTerrain();
}
private void DetermineTerrain()
{
RaycastHit[] hit;
hit = Physics.RaycastAll(transform.position, Vector3.down, 10.0f);
foreach (RaycastHit rayhit in hit)
{
if (rayhit.transform.gameObject.layer == LayerMask.NameToLayer(“Gravel”))
{
currentTerrain = CURRENT_TERRAIN.GRAVEL;
break;
}
else if (rayhit.transform.gameObject.layer == LayerMask.NameToLayer(“Wood”))
{
currentTerrain = CURRENT_TERRAIN.WOOD_FLOOR;
break;
}
else if (rayhit.transform.gameObject.layer == LayerMask.NameToLayer(“Grass”))
{
currentTerrain = CURRENT_TERRAIN.GRASS;
}
else if (rayhit.transform.gameObject.layer == LayerMask.NameToLayer(“Water”))
{
currentTerrain = CURRENT_TERRAIN.WATER;
}
}
}
public void SelectAndPlayFootstep()
{
switch (currentTerrain)
{
case CURRENT_TERRAIN.GRAVEL:
PlayFootstep(1);
break;
case CURRENT_TERRAIN.GRASS:
PlayFootstep(0);
break;
case CURRENT_TERRAIN.WOOD_FLOOR:
PlayFootstep(2);
break;
case CURRENT_TERRAIN.WATER:
PlayFootstep(3);
break;
default:
PlayFootstep(0);
break;
}
}
private void PlayFootstep(int terrain)
{
foosteps = FMODUnity.RuntimeManager.CreateInstance(“event:/Footsteps”);
foosteps.setParameterByName(“Terrain”, terrain);
foosteps.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(gameObject));
foosteps.start();
foosteps.release();
}
}

The ground objects are assigned to layers according to the script. But I can only hear the sounds coming from Parameter = 0 in the game, even though I’ve added different grounds in different layers. Is there something wrong with the code?

I’m using FMOD 2.00.10 and Unity 2020.1.10

Thanks for referring to my tutorial, but I can’t reproduce the problem in my example project, can you share a minimal project where the problem occurs? Keep in mind this is a Unity & programming question, not a problem about FMOD or the Unity integration.

Hi Alex, thanks for your reply and I apologize for misunderstanding the kind of problem I’ve got. The issue is that the footsteps sounds aren’t changing, even though my ground objects are assigned to respective layers, as in your guide. Debugging tells that the Terrain parameter changes inside Unity. But for some reason, it doesn’t change the sound module being played. It’s like Unity can’t talk to my FMOD when it comes to this parameter. I’ve tried to use your tutorial’s Fmod project and I face the same issue. It only playbacks Parameter = 0 sounds, despite the terrain.

Here’s my Unity Project
Here’s my FMOD Project

Change the function the animation events call from PlayFootstep to SelectAndPlayFootstep, then it will work. Cheers and happy developing :slight_smile:

1 Like

It works! My man… I really was looking forward to have that working. Thank you so much… And thank you for all your tutorials, I’m learning a lot from them!