简体   繁体   English

Unity Disorted Audio

[英]Unity Disorted Audio

I'm having issues playing Audio Clips in Unity. 我在Unity中播放音频剪辑时遇到问题。

I want my Shark to produce a "bite" sound when the player is detected but the sound is distorted. 当发现播放器但声音失真时,我希望我的鲨鱼产生“咬”声。

The rest of the code is running as intended. 其余代码按预期运行。

Can I have a code review and suggestion please? 我可以进行代码审查和建议吗?

What am I possibly doing wrong (when calling the Audio Source to Play)? 我可能做错了什么(在调用音频源播放时)?

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

One problem I see is that you're re-playing the sound at every screen update (based on your app's framerate). 我看到的一个问题是,您在每次更新屏幕时都会重新播放声音 (基于应用程序的帧速率)。 It's not clear if you want it to loop (since is placed inside a void Update function for repeated instructions) or you simply want it to play once per detection. 目前尚不清楚你是否希望它循环(因为它被放置在一个void Update函数中用于重复指令),或者你只是想让它在每次检测时播放一次。

If Unity can detect when a sound is playing or has finished then use that to help fix this. 如果Unity可以检测到声音播放或播放完毕,那么请使用它来帮助修复此问题。

The looping logic is: 循环逻辑是:

  • On each frame update, check if sound is already in "playing" mode. 在每次帧更新时,检查声音是否已处于“播放”模式。
  • If No... assume sound "ended" and you can now do bite.Play(); 如果不...假设声音“结束”,你现在可以做bite.Play(); .
  • Else-If Yes... let sound continue (maybe it'll be "ended" in a future frame check). 否则 - 如果是......让声音继续(也许它将在未来的帧检查中“结束”)。

A pseudo-code example: 一个伪代码示例:

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

For once-per-detection: 对于每次检测一次:

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

This line detect_Snd_Played = true; 这行detect_Snd_Played = true; should stop multiple playbacks until you reset. 应该停止多次播放,直到重置为止。 This could be if your player becomes "un-detected" by shark, and you can now reset for next time's new "detection" process... 这可能是因为你的玩家被鲨鱼“未检测到”,你现在可以重置为下一次的新“检测”过程......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM