Footstep sound does not finish playing, it keeps restarting on movement
When my player moves using WASD, the footstep sound clip keeps restarting without finishing the entire audio clip. I currently have an AudioManager script to handle all my audio files.
public class AudioManager : MonoBehaviour
{
public Sound[] sounds; //holds a list of audio clips
// Start is called before the first frame update
void Awake()
{
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
}
//find the sound by its name and play when called
public void PlayOneShot(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.Play();
}
}
Then in my Player Movement script, I am moving the player using the following code.
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
The code I am using to play the footstep audio is:
if (controller.isGrounded == true && controller.velocity.magnitude > 2f)
{
GetRunClip();
}
And:
void GetRunClip()
{
FindObjectOfType<AudioManager>().PlayOneShot("PlayerRun");
}
Comment