Question by
ghostyx101 · Dec 14, 2016 at 07:37 PM ·
colliderontriggerenteraudioclip
How to play audio only if i stay within a collider, and not only once
I have a game object where i want to play a song only when the player is within a collider, but i dont want it to stop happening everytime i enter it again. I have this so far, but it still stops..
using UnityEngine; using System.Collections;
public class SoundTrigger : MonoBehaviour { public AudioSource source; public AudioClip clip; private bool isPlayed;
public void Awake ()
{
source = GetComponent<AudioSource>();
isPlayed = false;
}
public void OnTriggerEnter(Collider other) {
if (!isPlayed)
{
source.Play();
isPlayed = true;
}
}
public void OnTriggerExit(Collider other)
{
if (source.isPlaying)
{
source.Stop();
}
}
}
Comment
Answer by hexagonius · Dec 14, 2016 at 07:46 PM
you only need Start and Stop in enter and exit respectively, that's it
Your answer
Follow this Question
Related Questions
C# OnTriggerEnter Issue 3 Answers
OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 Answers
OnTriggerEnter For Parking 1 Answer
Crane Pick up script - setting new parent 0 Answers
whats wrong with this code 2 Answers