Question by
Nosmo · Oct 18, 2017 at 05:40 PM ·
audioaudiosourceontriggerenterontriggerexitplayoneshot
Im trying to turn off a sound when i leave a trigger collider
I've written a script to play construction sounds when the player gets into a trigger collider and when they leave i want it to stop but it continues playing
What I've done so far:
public AudioSource SoundSource;
public AudioClip Sound;
public bool hasPlayedAudio = false;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag ("Player") && hasPlayedAudio == false)
{
SoundSource.PlayOneShot(Sound);
hasPlayedAudio = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag ("Player") && hasPlayedAudio == true)
{
SoundSource.PlayOneShot (Sound);
hasPlayedAudio = false;
}
}
}
I've made a check box that checks and unchecks with the player moves in and out of the trigger collider i just need the audio to stop when the player leaves the trigger.
I know i need to change the PlayOneShot in the OnTriggerExit. I just don't know to what.
Any thoughts?
Comment