在我的游戏中,我正在统一创建我试图在每次我的一个游戏对象发生碰撞时播放音频剪辑。出于某种原因,音频会在第一次发生碰撞时播放,但从那时起就不再播放声音了。
与之发生碰撞的游戏对象有一个 AudioSource 组件,我在该组件中选择了音频剪辑。
这是我开始播放音频的代码:
if (PlayerPrefs.GetInt ("Sound Playing", 1) == 1)
{
audio = GetComponent <AudioSource>();
audio.Play ();
}
我也尝试过使用 PlayOneShot () 方法,但它做同样的事情。
编辑
这里是我的游戏在这里做什么的一个小例子:
public class Ship : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D collider)
{
Laser laser = collider.gameObject.GetComponent<Laser> ();
if (laser)
{
if (!immune)
{
//update healthbar
healthbar.hit();
//reset health boost
forcefield.resetHealthBoost();
health -= laser.getDamage(); //remove health from ship
//create explosion
GameObject explosion = Instantiate(explode, gameObject.transform.position, Quaternion.identity) as GameObject;
explosion.GetComponent<ParticleSystem>().startColor = this.GetComponent<SpriteRenderer>().color;
if (SoundEffects.soundOn && PlayerPrefs.GetInt("Sound Playing", 1) == 1)
{
audio = GetComponent<AudioSource>();
audio.Play();
}
}
laser.hit();//destroy the laser that hit the ship
}
}
}
这是为了表明我在检查器中设置了 AudioSource 和 AudioClip(AudioSource 已连接到飞船):
最佳答案
我的游戏的问题是我没有意识到稍后在我的代码中使用 gameobject.SetActive(false) 会阻止第二个音频剪辑播放。当船被摧毁时,我将其设置为 false,这就是问题所在。现在工作正常。
关于c# - AudioClip 在 Trigger 上只播放一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32447134/