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?