Be the first to post for 'Player Damage & Death Sounds'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

I decided I did not like the way the audio trigger was cutting off damage sounds every time the player was hit so I set up a coroutine to only trigger a damage audio effect to trigger every 3 seconds no matter how many times the player was hit.

First I added a variable to the top as
bool playedDamageSoundRecently = false;

Then these are the methods I used.

// Damage interface
public void TakeDamage(float damage)
{
    bool playerDies = (currentHealthPoints - damage <= 0);
    ReduceHealth(damage);            
    if (!playedDamageSoundRecently)
    {
        StartCoroutine(PlayDamageSounds());
    }
    if (playerDies)
    {
        StartCoroutine(KillPlayer());
    }
}

IEnumerator PlayDamageSounds()
{
    playedDamageSoundRecently = true;
    audioSource.clip = damageSounds[UnityEngine.Random.Range(0, damageSounds.Length)];
    audioSource.Play();
    yield return new WaitForSecondsRealtime(3f);
    playedDamageSoundRecently = false;
}

IEnumerator KillPlayer()
{
    audioSource.clip = deathSounds[UnityEngine.Random.Range(0, deathSounds.Length)];
    audioSource.Play();
    yield return new WaitForSecondsRealtime(audioSource.clip.length);
    SceneManager.LoadScene(0);
}

This way the damage audio clips I have stopped cutting off whenever I was hit by a new projectile. Let me know if anyone else tries this and if it works for them as well.

You could also set the time between damage sounds to be audioSource.clip.length but I prefer a bit more of a break between the damage sounds. I may even increase it to 5 seconds.

Good job

Privacy & Terms