c# - 当 OnTriggerEnter() 播放声音时 Unity 编辑器冻结
问题描述
每次玩家进入触发器时,我的 Unity 编辑器都会冻结,
- 我添加了一个键码以查看是否是音频源引起的
- 我已经从 OnTriggerEnter 中注释了 source.Play() 并使用调试语句来检查碰撞是否正常工作
两者都可以正常工作,所以我知道这与 OnTriggerEnter() 激活源有关。
public class PlaySound : MonoBehaviour
{
public AudioSource source;
public Collider coll;
public AudioClip clip;
public bool soundPlaying;
private void Start()
{
source = GetComponent<AudioSource>();
coll = GetComponent<Collider>();
soundPlaying = false;
}
private void Update()
{
while(source.isPlaying)
{
soundPlaying = true;
}
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Space entered");
source.Play();
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
Debug.Log("player entered");
source.Play();
}
}
}
还有一点需要注意的是,soundPlaying Bool 是为了让 AI 检测物体,因为周围会有一些散落的物体。
解决方案
没关系,我解决了它,更新函数中的while循环可能导致溢出,所以我将其更改为if语句,现在一切正常
推荐阅读
- postgresql - 更改 postgres 表中的列顺序
- android - 在androidx生物识别提示中尝试三次失败后关闭提示?
- python - 检查单词中间是否有标点或数字
- python - 如何在 Python 中使用 AckID 确认 Google PubSub 消息
- machine-learning - 如何使用机器学习使用可用关键字对文本进行分类?
- typescript - react typescript eslint plugin - 文件(.svg)的扩展名是非标准的
- php - 使用 PHP 查找 IP 黑名单
- laravel-5.8 - 未找到特征'Carbon\Traits\ObjectInitialisation' laravel5.8
- react-native - 将参数化组件传递给 FlatList 的问题
- amazon-web-services - AWS ec2 暴露端口不适用于 Kubernetes 转发端口