Hello,
I am adding a basic sound function to my players script. its pretty easy, if he gets hit, he takes damage, and when his damage is lowered every 5% or so, he makes a noise.
It all works fine, but sometimes if he for instance takes damage and walks away from what hurt him (such as a fire), his “hurt sound” will keep rapid firing until I make him take damage again.
Would anyone know what is causing this bug? his health will stop declining when he leave the fire, but the noise keeps playing. its really weird.
Ive tried Audio.Stop but it just prevents the noise from playing. I tried doing a yield waitforseconds before audio.stop, but it gives me an error.
Would anyone know what may cause this?
Thank you.
Here is the script that says to play the audio sound when he his hurt.
var HurtSound : AudioClip;
var CurrentHP : float = 100.0;
private var MaximumHP : int = 100.0;
function Update ()
{
// plays when player takes damage
if (CurrentHP % 5 == 1)
{
audio.PlayOneShot(HurtSound);
}
hello, sorry for the late reply. I added a new bool and created an if statement like this: function Update () { if (CurrentHP % 5 == 1 && HurtSoundCanPlay == false) { audio.PlayOneShot(HurtSound); HurtSoundCanPlay = true; } else if (CurrentHP % 5 == 2 && HurtSoundCanPlay == true) { HurtSoundCanPlay = false; } It works just fine now. thank you for you help!
– Therian13