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(); */
}