Как воспроизвести звук только один раз / unity C#
У меня есть код на плеера. Пытаюсь ему сделать воспроизведение звука шагов (несколько).
Сделал через if (controller.isGrounded), но воспроизводятся все выбранные в инспекторе звуки, поэтому задаюсь вопросом: "Как воспроизвести звук только один раз". Помогите, пожалуйста.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour
{
public AudioClip[] impact;
private AudioSource audioSource;
public float speed = 1;
public CharacterController controller;
public Transform GroundCheck;
public LayerMask groundMask;
public float gravity = 9.8f;
public bool Sprint;
public float jumpHeight = 3f;
public float groundDistance = 0.4f;
public Animator anim;
Vector3 velocity;
bool isGrounded;
// Start is called before the first frame update
void Start()
{
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded)
{
anim.SetFloat("X", ControlFreak2.CF2Input.GetAxis("Horizontal"));
anim.SetFloat("Y", ControlFreak2.CF2Input.GetAxis("Vertical"));
audioSource.PlayOneShot(impact[Random.Range(0, impact.Length)]);
}
isGrounded = Physics.CheckSphere(GroundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = ControlFreak2.CF2Input.GetAxis("Horizontal");
float z = ControlFreak2.CF2Input.GetAxis("Vertical");
if(ControlFreak2.CF2Input.GetKeyDown(KeyCode.Space)&& isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * gravity);
anim.SetTrigger("Jump");
}
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (ControlFreak2.CF2Input.GetKey (KeyCode.W))
{
}
else
{
audioSource.Stop();
}
if (ControlFreak2.CF2Input.GetKey (KeyCode.W) && ControlFreak2.CF2Input.GetKey (KeyCode.LeftShift))
{
speed = 7f;
anim.SetFloat("Y", 2);
Sprint = true;
}
else
{
speed = 5f;
Sprint = false;
}
}
}