Audio play when enter trigger
I've never script anything with audio and trigger before so please help to make this script. I want to make a story-teller system, like:
-audio play when player enter trigger ( after enter trigger, the audio must be stil playing even the player is stil inside trigger or not)
-the trigger automaticcally disable (or u can say destroy itself) after the audio finished.
Special thanks to anyone who can help me with this one.
P/s: Sorry for my bad english
Answer by Ali-hatem · Mar 20, 2016 at 02:58 PM
attach this script to the triggers that player will enter & attach audio source & audio clip & make there collider component is Trigger in the inspector :
AudioSource audio1;
void Start(){
audio1 = GetComponent<AudioSource> ();
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player") {
StartCoroutine (PlayDestroy());
}
}
IEnumerator PlayDestroy(){
audio1.Play ();
yield return new WaitWhile (() => audio1.isPlaying);
Destroy (gameObject)
}
Your answer
Follow this Question
Related Questions
Wanting audio to pause on trigger enter 1 Answer
Question on Audio? 1 Answer
Dialogue Problem 0 Answers
How to enable and disable back game object for ui button after a few seconds? 1 Answer