Footstep Audio Playing on top of eachother when moving


#1

Hi folks,

New to FMOD and trying to figure out how to get footstep audio implemented probably, Within FMOD I have everything setup to play a random assortment of footsteps with each step however within UNITY, it seems to think that the player is always moving and not only loops the audio, but plays multiple instances of it.

I’ve attached my code below and would appreciate any help! Thanks!

public class TestFootStep : MonoBehaviour {

[FMODUnity.EventRef]
public string inputsound;
public bool playerIsMoving;
public float walkingSpeed;
float horizontal;
float vertical;
void Update()
{
Debug.Log (playerIsMoving);
if (Input.GetKey (KeyCode.A))
playerIsMoving = true;
if (Input.GetKey(KeyCode.D))
playerIsMoving = true;
if (Input.GetKey(KeyCode.W))
playerIsMoving = true;
if (Input.GetKey (KeyCode.S))
playerIsMoving = true;
else
playerIsMoving = false;
}

void FixedUpdate ()
{

}

void FootStepsAudio(){
if (playerIsMoving == true) {
FMODUnity.RuntimeManager.PlayOneShot (inputsound);
}
}

void Start (){
InvokeRepeating (“FootStepsAudio”, 0, walkingSpeed);
}
void OnDisable(){
playerIsMoving = false;

}
}


#2

Hi Kalen,

Please check for two things:

  1. According to the Unity docs, InvokeRepeating() is not meant to take 0 as a time scale. I was able to get this to work with: InvokeRepeating("FootStepsAudio", 0.5f, walkingSpeed);
  2. Ensure that your footstep event is truly a one shot. It should not contain any loop regions or transition regions that could cause it to repeat itself.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html

Thanks,
Richard