Footstep Sound Effect Unity 3D C#

  • Footstep Sound Effect Unity 3D C#

Here is a quick tutorial on how I added the footstep sound effect script to my dragon! You can apply this to your projects, but be sure to sub and like to see more videos. I often will throw a free script/tutorial along with my videos. See below to get the script and watch the video on how to plug it into your project.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FootStep_Sounds : MonoBehaviour {
[Header("THIS OBJECT SHOULD BE ATTACHED TO PARENT")]
[Header("BUT USE SEPARATE AUDIO SOURCE AS A CHILD AND ATTACH")]

public AudioSource audioSource;
public Animator anim;
public float stepDelay;
public AudioClip defaultClip;
public AudioClip defaultClip1;
public AudioClip defaultClip2;
private AudioClip currentClip;
private bool couroutineOn;

void Start () {

anim = this.gameObject.GetComponent ();

couroutineOn = true;
audioSource.clip = defaultClip;

StartCoroutine (Walking ());
}

IEnumerator Walking()
{

while (couroutineOn == true) {

if (anim.GetCurrentAnimatorStateInfo (0).IsName ("Run") || anim.GetCurrentAnimatorStateInfo (0).IsName ("Walk") || anim.GetCurrentAnimatorStateInfo (0).IsName ("Stealth")) {

int rand = Random.Range (0, 2);
if (rand == 0) {
currentClip = defaultClip;
} else if (rand == 1) {
currentClip = defaultClip1;
} else {
currentClip = defaultClip2;
}

audioSource.clip = currentClip;

audioSource.Play ();

} else {

audioSource.Stop ();
}

yield return new WaitForSeconds (stepDelay);

}
}
}