Playing footsteps audio not working

hello I’m a beginner, I tried to put a footsteps audio in my game whenever the player walks (his speed is higher than 0.001), by attaching a audio source to it with loop checked. Here’s the code:

If (rb.velocity.x > 0.001f && isgrounded == true)
{
   audiosource.PlayOneShot(audioclip);
}

If (rb.velocity.x < 0.001f)
{
   audiosource.Stop();
}

I did a debug.log test and found out that only audio.Stop works, but it doesn’t play at the first place. What am I doing wrong? Thanks for your time.

Not sure about your audio problem, but ‘If’ has to be ‘if’. Capitalization is important.

1 Like

Oh I know this, it’s capitalized because I couldn’t copy the original script from my pc so I wrote it on my phone oop
So there’s nothing wrong with my script?

There could be a lot wrong with it. isGrounded might always be false if you’re not doing your ground test properly. The velocity might be 0 because you’re not moving the rigidbody properly. More in general, it’s almost certainly wrong to be calling that code every frame, or you’ll get 60+ footstep sounds per second. So you’d want to build in some delay in playing the audio clip again after playing it once. It’s possible that your audio clip starts with a tiny bit of silence, and since you’re calling this every frame, it’s constantly restarting the clip, and not apparently making any sound.

Anyway, could be lots of problems. Use Debug.Log to check one thing at a time to make sure your assumptions are correct.

1 Like

So, I went back to the script and instead of using the velocity, I made
3 vector2 variables to calculate the speed. previous and new position, divide the two and you get the speed, and it works!
However there’s two problems, the audio only plays when I’m going right and doesn’t when I’m going left.
The other problem is the audio sounds really wrong, like heavily distorted Or something and I listened to it in the inspector alone, and I’m sure it isn’t the problem. The exact problem happened when I put a sound on a ladder. I also have music running in the background but it sounds just fine. Thx for the help

I don’t think it makes mathematical sense to divide one position by another. It sounds like you’ve just got some weird coincidence where that seems to be giving you reasonable output by accident. If you’re going to compute “velocity” based on two positions, you’d get the difference between them and divide by the amount of time that passed (Time.deltaTime). That would give you the velocity. You can then take the magnitude of that velocty to get the “speed”. It’s possible the reason you’re not getting footsteps when moving left is because you’re expecting the velocity to be positive, but velocity will be negative when moving left. Remember that speed != velocity.

As for the audio sounding weird, I’d make sure it plays fine on a plain old audio source in the level. Just add a new audio source, put the clip on it, and rig it up to play the clip when you press some arbitrary key. As long as that works fine, you can look more closely at why the other one isn’t playing correctly. You’re sure you’re not still telling it to play every single frame?

Ok I meant to say subtract not divide (sorry English isn’t my first language) and I divided them from time.deltatime. How do I get the negative velocity?
And where should I put playing the audio? Right now it’s in fixedupdate

Velocity is already negative if the object is going left. “Velocity” means the direction and speed, because it’s a “vector”. If you’re moving right at 1 m/s, your velocity is 1 m/s on the x-axis. If you’re moving left at 1 m/s, your velocity is -1 m/s on the x-axis. However, your “speed” in both cases is 1 m/s. Speed is never negative. To get the “speed” of a vector, you just take its “magnitude” property. That’s always positive. So, to test if something is moving more than 1 m/s in any direction, you’d check the magnitude of the velocity.

You can put the playing of the audio wherever you want. But I wanted you to test that it worked properly in an isolated test. Just put something in Update() where if you press the “J” key, it plays the audio clip once. As long as it plays properly, then you must have an error in the way you’re playing your audio in the other case. Post that code if it’s still a problem.

1 Like

Thanks for the help! I realized that -1 is smaller than -0.001 and that was the problem in the code, so now the audio plays in both ways.
But the problem with the audio it self I can’t figure it out, I did the j test you told me to do and I found out somethings, when the audio is put to play on awake it plays perfectly. My footsteps code keeps running it in every frame just like you said, but even if I play it once it still sounds wrong.

https://www.youtube.com/watch?v=d7ruhquR904

I managed to film what it sounds like along with a view on the inspector. Does this have to do with the code or my unity version? Thx again
Ps. I turned of “AudioSource.Stop();” so I show you what’s happening properly

That sounds very much like you’re playing the audio source 100 times, starting each one a frame apart from each other.

You’re calling `audiosource.PlayOneShot’ in your Update() or FixedUpdate() method, probably any time the movement speed exceeds a certain amount? What that means is that if you hold the left or right movement keys, then every 1/60th of a second, that code will get run, and you’ll play a new copy of that audio clip. What you’re hearing is likely the sound of 60+ audio clips overlapping, each offset from each other by a frame.

You need to throttle your footstep sounds. Something like this:

const float _timeBetweenFootsteps = 0.5f;
float _lastPlayedFootstepSoundTime = -_timeBetweenFootsteps;

void Update() {
    if (movementSpeed > 1) {
        if (Time.timeSinceLevelLoad - _lastPlayedFootstepSoundTime > _timeBetweenFootsteps) {
            audioSource.PlayOneShot(clip);
            _lastPlayedFootstepSoundTime = Time.timeSinceLevelLoad;
        }
    }
}

Change _timeBetweenFootsteps to how long you want between footsteps. This will then play the audio clip only if the player is moving fast enough and it hasn’t played the audio clip within the last 0.5 seconds.

2 Likes