gpt4 book ai didi

c# - Unity 失真音频

转载 作者:行者123 更新时间:2023-11-30 17:31:28 26 4
gpt4 key购买 nike

我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com