Sound Issues with animation curves

I’m having a bit of difficulty coordinating my sounds with the following code :

enter code hereusing UnityEngine;
using System.Collections;

public class PlayerSounds : MonoBehaviour {

	public AudioClip [] swordSwings;
	public AudioClip drawSword;
	public AudioClip holsterSword;
	public AudioClip [] swordImpacts;
	public AudioClip [] clubImpacts;
	public AudioClip bowDraw;
	public AudioClip bowRelease;
	public AudioClip fireRifle;
	
	public AudioClip [] footStepSoundGrass;
	
	Animator _animator;
	AudioClip _currentClip;
	float _footFloat;
	bool _delay;	
	AudioClip _currentSound;
	
	void Start() {
		_animator = transform.GetComponent<Animator>();	
		_footFloat = _animator.GetFloat ("footstepcurve");
	}
	
	public void PlaySoundSwordSwing() {
		_currentSound = swordSwings[Random.Range (0,swordSwings.Length)];
		audio.PlayOneShot(_currentSound);
	}
	
	public void PlayDrawSwordSound() {
		audio.PlayOneShot(drawSword);
	}
	
	public void PlayHolsterSwordSound() {
		audio.PlayOneShot(holsterSword);
	}
	
	public void PlaySwordImpact() {
		_currentSound = swordImpacts[Random.Range (0,swordImpacts.Length)];
		audio.PlayOneShot(_currentSound);		
	}
	
	public void PlayClubImpact() {
		_currentSound = clubImpacts[Random.Range (0,clubImpacts.Length)];
		audio.PlayOneShot(_currentSound);	
	}
	
	public void PlayBowDrawSound() {
		audio.PlayOneShot(bowDraw);
	}
	
	public void PlayBowReleaseSOund() {
		audio.PlayOneShot(bowRelease);
	}
	
	public void PlayRifleSOund() {
		audio.PlayOneShot (fireRifle);	
	}
	
	void Update() {
		_footFloat = _animator.GetFloat ("footstepcurve");
		
		if (_footFloat > .05f) {
			Debug.Log (_footFloat);
			//audio.PlayOneShot (footStepSound);
			if (!_delay)
				StartCoroutine ("PlayFootStepSound");
		}
		if (_footFloat < -.05f) {
			Debug.Log (_footFloat);
			//audio.PlayOneShot (footStepSound);
			if (!_delay)
				StartCoroutine ("PlayFootStepSound");			
		}
	}
	
	IEnumerator PlayFootStepSound() {
		_currentSound = footStepSoundGrass[ Random.Range (0, footStepSoundGrass.Length)];
		//_currentClip = footStepSoundGrass[ Random.Range (0, footStepSoundGrass.Length)];
		
		_delay = true;
		yield return new WaitForSeconds(.1f);
//		audio.PlayOneShot (_currentClip);
		audio.PlayOneShot (_currentSound);
		_delay = false;
	}	
}

The code is somewhat lengthy repetitive and is used for some basic sounds for my character. In the Update I have some code that plays footsteps and works fine. However, once I play any other sound the footstep sounds no longer play. I am not sure why. I have a few debug.logs in the Update and they don’t seem to register if I run any of the other sounds out of the Update. Can anyone advise me on what I am doing wrong?

“once I play any other sound the footstep sounds no longer play. I am not sure why.”

Just tested out what you did, it does stop, but then if you back up it returns, as if it’s following you and you’re ahead of it! Instead of using PlayOneShot, I used the static function AudioSource.PlayClipAtPoint which seemed to fixed it - What this does, is instantiate an AudioSource at the position you feed it, to play a given clip. When the clip is done playing, the source is destroyed. In your case, I think you’d want to play all the sounds from the player, so feed it your cachedTransform.position

“The code is somewhat lengthy repetitive”

Here’s what you can do about that:

Remove all those redundant clip playing methods (PlayClubImpact, PlayBowDrawSound, etc) and create a PlayClip and PlayRandomClip static methods (Put them in a utility class or something)

public static void PlayClip(AudioClip clip, Vector3 position)
{
   AudioSource.PlayClipAtPoint(clip, position);
}

public static void PlayRandomClip(AudioClip clips[], Vector3 position)
{
   PlayClip(clips[Random.Range(0, clips.Length)], position);
}

If you think there’s also gonna be repetitiveness now calling these with PlayClip(clip, transform.position); every time, you could write an extension method for the Transform class:

public static class TransformExtensions
{
    public static void PlayClip(this Transform t, AudioClip clip)
    {
        MyUtilities.PlayClip(clip, t.position); // assuming that's where you put the previous PlayClip static function
    }

    // do the same for the random one
}

Now you can do, myTransform.PlayClip(clip);

And please don’t use the string version of StartCoroutine, that looks awful. Just use StartCoroutine(myMethod(args));