Unity 失真音频
Unity Disorted Audio
我在 Unity 中播放音频剪辑时遇到问题。
我希望我的 Shark 在检测到玩家但声音失真时发出 "bite" 声音。
其余代码运行符合预期。
我可以进行代码审查和建议吗?
我可能做错了什么(调用音频源播放时)?
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 可以检测到声音何时播放或结束,则使用它来帮助解决此问题。
循环逻辑为:
- 在每次帧更新时,检查声音是否已经处于 "playing" 模式。
- 如果否...假定声音 "ended" 您现在可以
bite.Play();
.
- Else-If Yes...让声音继续(也许在未来的帧检查中会"ended")。
伪代码示例:
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;
应停止多次播放,直到您重置。这可能是如果您的播放器成为 "un-detected" 鲨鱼,您现在可以重置下一次的新 "detection" 进程...
我在 Unity 中播放音频剪辑时遇到问题。
我希望我的 Shark 在检测到玩家但声音失真时发出 "bite" 声音。
其余代码运行符合预期。
我可以进行代码审查和建议吗?
我可能做错了什么(调用音频源播放时)?
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 可以检测到声音何时播放或结束,则使用它来帮助解决此问题。
循环逻辑为:
- 在每次帧更新时,检查声音是否已经处于 "playing" 模式。
- 如果否...假定声音 "ended" 您现在可以
bite.Play();
. - Else-If Yes...让声音继续(也许在未来的帧检查中会"ended")。
伪代码示例:
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;
应停止多次播放,直到您重置。这可能是如果您的播放器成为 "un-detected" 鲨鱼,您现在可以重置下一次的新 "detection" 进程...