Unity 扭曲音频

3

我在Unity中播放音频片段时遇到了问题。

当检测到玩家时,我希望我的鲨鱼能够发出“咬”的声音,但声音却变形了。

其余的代码都按预期运行。

请问我能够进行代码审查并提供建议吗?

我可能在哪里出了错(调用音频源播放时)?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shark2Controller : MonoBehaviour {

    public Transform leftPoint;
    public Transform rightPoint;

    public float startSpeed;
    public float swimSpeed;
    private Rigidbody2D myRigidBody;
    public bool swimmingRight;


    public float superSpeed;
    public Transform puntoA;
    public Transform puntoB;
    public LayerMask whatIsPlayer;
    public bool playerDetected;

    private Animator myAnim;

    public AudioSource bite;

    // Use this for initialization
    void Start ()
    {
        myRigidBody = GetComponent<Rigidbody2D> ();
        myAnim = GetComponent<Animator>();
        swimSpeed = startSpeed;
    }

    // Update is called once per frame
    void Update () 
    {
        playerDetected = Physics2D.OverlapArea (puntoA.position, puntoB.position, whatIsPlayer);
        myAnim.SetBool ("Player Detected", playerDetected);

        if (playerDetected) 
        {
            swimSpeed = superSpeed;
            bite.Play ();
        } 


        if (swimmingRight && transform.position.x > rightPoint.position.x) 
        {
            swimmingRight = false;
        }

        if (!swimmingRight && transform.position.x < leftPoint.position.x) 
        {
            swimmingRight = true;
        }



        if (swimmingRight) 
        {
            myRigidBody.velocity = new Vector3 (swimSpeed, myRigidBody.velocity.y, 0f);
            transform.localScale = new Vector3 (1f, 1f, 1f);
        }
        else 
        {
            myRigidBody.velocity = new Vector3 (-swimSpeed, myRigidBody.velocity.y, 0f);
            transform.localScale = new Vector3 (-1f, 1f, 1f);
        }
    }

    public void ResetShark2Speed()
    {
        swimSpeed = startSpeed;
    }
}

但声音变形了。是哪种变形,音量级别的扭曲吗?此外,bite.Play(); 应该在每个帧更新时触发吗?如果您的FPS为60,您真的想让 bite 每秒播放60次吗?那听起来不太好,我期望会有(可能的)嗡嗡声/卡顿的噪音……也许这就是你所说的变形? - VC.One
没错,那是嗡嗡声。非常感谢您的解释,我之前对在Update()期间使用Audio Source不太清楚。 - Rub
1个回答

2
我看到的一个问题是,你在每次屏幕更新时都会重新播放声音(根据应用程序的帧率)。不清楚你是否想要循环播放它(因为它被放置在void Update函数中以重复指令),还是只想要每次检测时播放一次。
如果Unity可以检测到声音何时正在播放或已完成,则可以使用它来帮助解决此问题。
循环逻辑如下:
- 在每个帧更新时,检查声音是否已处于“播放”模式。 - 如果没有...假设声音“结束”,现在可以执行bite.Play();。 - 否则-如果是...让声音继续(也许它将在将来的帧检查中“结束”)。
伪代码示例:
if (playerDetected == true) 
{
    swimSpeed = superSpeed;

    if( bite.isPlaying == true)
    {
        //# Do nothing or whatever you need
    }
    else 
    {
        //# Asssume it's not playing (eg: stopped, ended, not started, etc)
        bite.Play (); 
    }
} 

对于一次检测:

public bool detect_snd_Played;

detect_Snd_Played = false;  //# Not yet played since no "detect"

//# Update is called once per frame
void Update () 
{
    if (playerDetected == true) { swimSpeed = superSpeed; }

    if (detect_Snd_Played == false) 
    {
        bite.Play ();
        detect_Snd_Played = true; //# Stops future playback until you reset to false
    }
}

这行代码 detect_Snd_Played = true; 应该能够防止多次播放直到你重置。这可能是因为你的播放器被鲨鱼“未检测”了,现在你可以为下一次新的“检测”过程进行重置…


哇,非常感谢您如此迅速的回复和令人难以置信的解释。我希望声音效果只在检测到玩家时播放,所以我将使用第二段代码。我是Unity的新手,这个音频剪辑问题一直困扰着我,这对我来说非常有用:) 谢谢朋友! - Rub
因为我想要在检测到玩家时声音循环播放,所以我使用了第一段代码,并且运行得非常完美,再次感谢您! - Rub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接