【问题标题】:Why is this audio source not playing anything?为什么这个音频源不播放任何东西?
【发布时间】:2021-10-24 23:02:06
【问题描述】:

我正在尝试编写一个脚本,其中音频源播放一个包含 4 个声音的音频剪辑数组。当我的角色在冲刺时,声音会播放得更快更响,而当我的角色蹲下时,声音会变得更安静和更慢。我的问题是音频源不播放音频剪辑。查了好几遍了,真的找不到问题。

`public class PlayerFootsteps : MonoBehaviour { 私有 AudioSource 脚步声;

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

}

【问题讨论】:

    标签: c# visual-studio unity3d audio audio-source


    【解决方案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
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      • 2017-07-12
      • 2016-06-21
      • 2017-12-20
      • 2013-04-06
      相关资源
      最近更新 更多