【问题标题】:Unity3d - Audio rough sound when transition between play and pauseUnity3d - 在播放和暂停之间转换时的音频粗糙的声音
【发布时间】:2017-11-15 19:29:28
【问题描述】:

我有一个 Unity 脚本,可以在玩家移动时播放音频(脚步声)。该脚本正在运行,但还有一个问题,在播放和暂停之间转换时,音频似乎太粗糙(并且可能会伤害我的扬声器呵呵)。

这是我的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class PlayerController : MonoBehaviour {
    Rigidbody playerRigid;
    private Vector3 pindah;
    public AudioSource audioSource;
    public AudioClip audioClip;
    public float lowPitchRange = .95f;
    public float hiPitchRange = 1.05f;

    void Awake () {
        audioSource = GetComponent<AudioSource> ();
        audioSource.clip = audioClip;
        float randomPitch = Random.Range (lowPitchRange, hiPitchRange);
        audioSource.pitch = randomPitch;
    }

    // Use this for initialization
    void Start () {
        //mengambil komponen rigidbody player
        playerRigid = GetComponent<Rigidbody> ();
    }

    // Update is called once per frame
    void Update () {
        //untuk mendapatkan input dari virtual joystick button
        float h = CrossPlatformInputManager.GetAxisRaw ("Horizontal");
        float v = CrossPlatformInputManager.GetAxisRaw ("Vertical");

        if ((h != 0f || v != 0f) && !audioSource.isPlaying) {
            audioSource.Play ();
        } else {
            audioSource.Pause ();
        }
        //memindah posisi player sesuai input yang didapat
        pindah = new Vector3 (h, 0.0f, v);
        playerRigid.velocity = pindah;
    }
}

【问题讨论】:

    标签: c# unity3d audio unity3d-5


    【解决方案1】:

    如果问题是暂停时音频源弹出,也许你应该考虑使用类似这样的东西来淡化音量(我没有测试过自己,我会稍后尝试):

    // fade/increase time in seconds
    public float rateTime = 1; 
    
    public FadeSound() { 
        //audioSource.Play ();
        StartCoroutine(_FadeSound); 
        }
    
    public IncreaseSound() { 
        StartCoroutine(_IncreaseSound); 
    }
    
    IEnumerator _FadeSound() {
        float t = rateTime;
    
        while (t > 0) {
            yield return null;
            t-= Time.deltaTime;
            source.volume = t/rateTime;
        }
        yield break;
        //yield audioSource.Pause ();
    }
    
    IEnumerator _IncreaseSound() {
        float t = 0;
    
        while (t < 1) {
            yield return null;
            t+= Time.deltaTime;
            source.volume = t/rateTime;
        }
        yield break;
    }
    
    if ((h != 0f || v != 0f) && !audioSource.isPlaying) {
        IncreaseSound();
    } else {
        FadeSound();
    }
    

    【讨论】:

    • 通常最好从编辑参数的当前值开始这种平滑过渡,在这种情况下是 source.volume。否则,如果您在 FadeSound 和 increaseSound 之间切换得太快,因为音量会立即跳到过渡的起始值 0 或 1,您会得到爆音。您可能还需要在开始新的协程之前停止任何冲突的协程。跨度>
    • 感谢matti的反馈,今天下午我会更新答案。我觉得这不是用户所期望的,但无论如何它对我很有用。谢谢!!
    【解决方案2】:

    我猜你需要像这样更改代码:

    if (h != 0f || v != 0f) {
         if (!audioSource.isPlaying) audioSource.Play ();
    } else {
         audioSource.Pause ();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2016-02-28
      • 1970-01-01
      相关资源
      最近更新 更多