I Am trying to make a step sound that will make a step sound that will play when the players move.And I cant do that.I need help :/(Without animation)
The Unity Standard Assets package has some footstep audio examples. Don’t know if they do or do not rely on animation.
If they DO rely on animation, then you can do it without: you just need a script that in Update() adds your distance moved this frame (compared to last frame) and when it exceeds what you consider a footstep distance, play a footstep audio and reset the distance counter.
As for the actual playing audio part, it’s as simple as adding an AudioSource (built-in to Unity) component to your GameObject, adding an AudioClip to it (sound file) and calling PlayOneShot() at the appropriate time.
To add onto this using a coroutine to have some delay between playing the sounds would be a good idea. Just add checks to make sure the player is moving in the coroutine so it doesn’t play footsteps when the player isn’t moving.
[RequireComponent(typeof(AudioSource))]
public class Example : MonoBehavior {
// State variables
private bool _isMoving;
private bool _coroutineIsRunning;
// Cached Component Reference
private AudioSource _audioSource;
// Constant
private const int FootstepDelay = 1;
private void Awake() => _audioSource = GetComponent<AudioSource>();
private IEnumerator PlayFootstepSound() {
_coroutineIsRunning = true;
_audioSource.PlayOneShot(_audioSource.clip, 1f);
yield return new WaitForSeconds(FootstepDelay)
_coroutineIsRunning = false;
}
private void RigidbodyMovement() {
if (!_isMoving) return;
// Movement code
if (!_coroutineIsRunning && _audioSource.clip) StartCoroutine(PlayFootstepSound());
_isMoving = false;
}
private void UpdateIsMoving() {
if (Any movement key is pressed) _isMoving = true;
}
private void Update() => UpdateIsMoving();
private void FixedUpdate() => RigidbodyMovement();
}
I haven’t tested the code but that should work perfectly. Hope it helps!