c# - Unity 失真音频

标签 c# unity3d audio-player

我在 Unity 中播放音频剪辑时遇到问题。

我希望我的 Shark 在检测到玩家但声音失真时发出“咬”的声音。

其余代码按预期运行。

我可以进行代码审查和建议吗?

我可能做错了什么(调用音频源播放时)?

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

最佳答案

我看到的一个问题是您在每次屏幕更新时重新播放声音(基于您应用的帧率)。目前尚不清楚您是希望它循环播放(因为它被放置在 void Update 函数中用于重复指令)还是您只是希望它在每次检测时播放一次。

如果 Unity 可以检测到声音何时播放或结束,则使用它来帮助解决此问题。

循环逻辑是:

  • 在每次帧更新时,检查声音是否已处于“播放”模式。
  • 如果否...假设声音“结束”,您现在可以执行 bite.Play();
  • Else-If Yes...让声音继续(也许它会在未来的帧检查中“结束”)。

伪代码示例:

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; 应该停止多次播放,直到您重置。这可能是如果您的播放器被鲨鱼“未检测到”,您现在可以重置以进行下一次新的“检测”过程...

关于c# - Unity 失真音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47806912/

相关文章:

c# - 删除资源字典中未使用的样式

c# - 如果我扩展一个 c# 接口(interface),我可以吃一个电话吗?

c# - 值为 null,但检查返回其他值?

java - Unity3D - 从 Android 调用静态方法

c# - restsharp 发出多个异步请求

c# - 为本地主机上的任何端口启用 CORS

c# - 如何通过单击 C# 中的按钮从另一个表单中隐藏一个表单?

automation - 基于收听习惯的自动音乐评级

javascript - 播放按钮动画

python - pygame.错误: set_pos unsupported for this codec