- Home /
Question by
KUFgoddess · Mar 07, 2017 at 04:21 PM ·
deathloopingsound effects
How to make player death sound effect / hitsound play once when function is called?
Hello, my player keeps looping their death sound / hit sound when they die in game I was wondering how I could make the player death / hit sound only play once. Right now its playing the death / hit sound but it is looping and overlapping rapidly and it's quite unpleasant to listen to.
private bool alreadyPlayed;
public AudioClip SoundToPlay;
public float Volume;
void Die()
{
charController.GetComponent<CharacterController>().enabled = false;
GetComponent<AudioSource>().PlayOneShot(SoundToPlay, Volume);
alreadyPlayed = true;
gameOverScreen.SetActive(true);
Debug.Log("You Have Died");
GetComponent<AudioSource>().Stop();
//do something here!
/*GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject enemy in enemies)
{
NavMeshAgent agent = enemy.GetComponent<NavMeshAgent>();
if (!agent)
continue;
agent.Stop(); */
}
Comment
Best Answer
Answer by dreamriver · Mar 07, 2017 at 06:48 PM
why don't you put
void Die() { if (alreadyPlayed) return;
charController.GetComponent().enabled = false; GetComponent().PlayOneShot(SoundToPlay, Volume); alreadyPlayed = true;
Your answer
Follow this Question
Related Questions
Sound effect when health=0? 2 Answers
audio synthesizer in unity? 1 Answer
Animation Keeps Looping 2 Answers
How To Wrap A Blender Animation??? 1 Answer
Transforming position for different colliders for repeating background? 0 Answers