so i wrote this script to play a footstep sound whenever my players speed was more than 0, but it seems that it keeps playing this sound over and over again and the sound clip just stacks on top of itself. any ideas on how to make it only play once?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playercontroller : MonoBehaviour
{
Rigidbody2D body;
public AudioSource playeraudio;
public AudioClip woodwalk;
float horizontal;
float vertical;
public float runSpeed = 20.0f;
void Start ()
{
body = GetComponent<Rigidbody2D>();
}
void Update ()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
}
private void FixedUpdate()
{
body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
if (body.velocity.magnitude > 0)
{
playeraudio.PlayOneShot(woodwalk);
}
}
}