Playing audio only once

if (distance <= 3)
    {
        sanityGUI.currentSanity = sanityGUI.currentSanity + sanityDamage.scarePercent;
    }
    else if(distance <= 9)
    {
        audioCanPlay = true;
    }
    else if (distance <= 20)
    {
        anim.SetFloat ("Speed", 1.8f);
        anim.SetBool("Running", true);
        anim.SetBool("Walking", false);
    }
    else if (distance <= 999 && distance > 20)
    {
        anim.SetFloat ("Speed", 0.8f);
        anim.SetBool("Running", false);
        anim.SetBool("Walking", true);
    }
   
    if(audioCanPlay == true)
    {
        AudioSource.PlayClipAtPoint(ScareSound, enemy.position);
    }

I’m using the following to check distance from the player, but I can’t figure out how to only trigger the sound once instead of multiple times. PlayClipAtPoint does a OneShot audio clip, but it plays constantly if distance is less than 9. setting it equal to 9 makes it not trigger at all. Can anyone tell me what I can do?

1 Like

add another bool “hasPlayed”, default to false on start, check it if it’s false in line 22, set it to true after line 24

1 Like