Why is this audio source not playing anything?

67 Views Asked by At

Im trying to write a script where the audio source plays an audioclip array with 4 sounds in it. The sounds are gonna be played faster and louder when my character is sprinting and quieter and slower when my character is crouching. My problem is that the audiosource doesn't play the audioclip. I've checked it multiple times and I really can't find the problem.

`public class PlayerFootsteps : MonoBehaviour { private AudioSource footstepSound;

[SerializeField]
private AudioClip[] footstepClip;

private CharacterController charController;

[HideInInspector]
public float VolumeMin, VolumeMax;

private float accumulatedDistance;

[HideInInspector]
public float stepDistance;


void Awake()
{
    footstepSound = GetComponent<AudioSource>();
    charController = GetComponentInParent<CharacterController>();
}

void Update()
{
    CheckToPlayFootstepSounds();
}

void CheckToPlayFootstepSounds()
{
    if (!charController.isGrounded)
    {
        return;
    }

    if (charController.velocity.magnitude > 0)
    {
        accumulatedDistance += Time.deltaTime;

        if (accumulatedDistance > stepDistance)
        {
            footstepSound.volume = Random.Range(VolumeMin, VolumeMax);

            footstepSound.clip = footstepClip[Random.Range(0, footstepClip.Length)];

            footstepSound.Play();

            accumulatedDistance = 0f;
        }
        else
        {
            accumulatedDistance = 0f;
        }
    }
}

}`

public class PlayerSprintAndCrouch : MonoBehaviour

{

private PlayerMovement playerMovement;

public float sprintSpeed = 10f, moveSpeed = 5f, crouchSpeed = 2f;

[SerializeField]
private Transform lookRoot;

private float standHeight = 1.6f, crouchHeight = 1f;

private bool isCrouching;

private PlayerFootsteps playerFootsteps;
private float sprintVolume = 1f;
private float crouchVolume = 0.1f;
private float walkVolumeMin = 0.2f, walkVolumeMax = 0.6f;

private float walkStepDistance = 0.4f, sprintStepDistance = 0.25f, crouchStepDistance = 0.5f;

void Awake()
{
    playerMovement = GetComponent<PlayerMovement>();

    playerFootsteps = GetComponentInChildren<PlayerFootsteps>();
}

void Start()
{
    playerFootsteps.VolumeMin = walkVolumeMin;

    playerFootsteps.VolumeMax = walkVolumeMax;

    playerFootsteps.stepDistance = walkStepDistance;
}

// Update is called once per frame
void Update()
{
    Sprint();
    Crouch();
}

void Sprint()
{
    if (Input.GetKeyDown(KeyCode.LeftShift) && !isCrouching)
    {
        playerMovement.speed = sprintSpeed;

        playerFootsteps.stepDistance = sprintStepDistance;
        playerFootsteps.VolumeMin = sprintVolume;
        playerFootsteps.VolumeMax = sprintVolume;

    }

    if (Input.GetKeyUp(KeyCode.LeftShift) && !isCrouching)
    {
        playerMovement.speed = moveSpeed;

        playerFootsteps.stepDistance = walkStepDistance;
        playerFootsteps.VolumeMin = walkVolumeMin;
        playerFootsteps.VolumeMax = walkVolumeMax;

    }
}

void Crouch()
{
    if (Input.GetKeyDown(KeyCode.LeftControl))
    {

        if (isCrouching)
        {
            lookRoot.localPosition = new Vector3(0f, standHeight, 0f);
            playerMovement.speed = moveSpeed;

            isCrouching = false;
        }
        else
        {
            lookRoot.localPosition = new Vector3(0f, crouchHeight, 0f);
            playerMovement.speed = crouchSpeed;

            playerFootsteps.stepDistance = crouchStepDistance;
            playerFootsteps.VolumeMin = crouchVolume;
            playerFootsteps.VolumeMax = crouchVolume;

            isCrouching = true;
        }
    }
}

}

1

There are 1 best solutions below

0
On BEST ANSWER

The problem is that you are setting the accumulatedDistance to 0, This should fix your problem:

void CheckToPlayFootstepSounds()
{
    if (!charController.isGrounded)
    {
        return;
    }

    if (charController.velocity.magnitude > 0)
    {
        accumulatedDistance += Time.deltaTime;

        if (accumulatedDistance > stepDistance)
        {
            footstepSound.volume = Random.Range(VolumeMin, VolumeMax);

            footstepSound.clip = footstepClip[Random.Range(0, footstepClip.Length)];

            footstepSound.Play();

            accumulatedDistance = 0f;
        }
        else
        {
            // accumulatedDistance = 0f; Remove This
        }
    }
}