0

我正在尝试编写一个脚本,其中音频源播放一个包含 4 个声音的音频剪辑数组。当我的角色在冲刺时,声音会播放得越来越快,而当我的角色蹲下时,声音会越来越慢。我的问题是音频源不播放音频剪辑。我检查了很多次,我真的找不到问题。

`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;
        }
    }
}

}

4

1 回答 1

1

问题是您将累积距离设置为 0,这应该可以解决您的问题:

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
        }
    }
}
于 2021-08-24T18:40:01.097 回答