单击按钮时出现音频播放问题
本文关键字:音频 播放 问题 按钮 单击 | 更新日期: 2023-09-27 18:26:16
我正在处理unity,音频有问题。这是当用户点击按钮时,物体掉在地上并被摧毁的场景。当点击按钮时,播放物体坠落的声音效果。并且销毁,对象再次实例化然后再次播放相同的点击音效。但当一个物体掉落并且此时没有碰撞时,用户再次点击该按钮会再次播放声音。我希望当一个物体被完全摧毁时,再次点击并播放声音
代码立方体脚本:
public class Cube : MonoBehaviour {
Rigidbody2D body;
void Start () {
body = GetComponent<Rigidbody2D>();
body.isKinematic = true;
}
}
代码碰撞脚本:
public class Ground : MonoBehaviour {
private Button bt;
public GameObject cube;
public AudioSource source;
public AudioClip clip;
void Start () {
bt = GameObject.FindGameObjectWithTag ("Button").GetComponent<Button> ();
bt.onClick.AddListener (() => Fall ());
}
void OnCollisionEnter2D(Collision2D col) {
Destroy (col.gameObject);
Instantiate (cube,new Vector3(0f,4.19f,0f),Quaternion.identity);
}
public void Fall(){
GameObject.FindGameObjectWithTag ("Player").GetComponent<Rigidbody2D> ().isKinematic = false;
source.PlayOneShot(clip);
}
}
void OnCollisionEnter2D(Collision2D col) {
Destroy (col.gameObject);
Instantiate (cube,new Vector3(0f,4.19f,0f),Quaternion.identity);
isFalling = false; // here
}
private bool isFalling = false; // here
public void Fall()
{
GameObject.FindGameObjectWithTag ("Player").GetComponent<Rigidbody2D> ().isKinematic = false;
if(isFalling == false){
source.PlayOneShot(clip);
isFalling = true; // here
}
}
几乎当你按下时,它会叫Fall,如果没有东西掉下来,就会发出声音。碰撞时,isFalling将重置。但是我对你的逻辑不完全确定。