Good afternoon everyone !
I wanted to create an event that call a script according to a music. The concept is simple. At x:xx of the music, I want to see this event.
I linked the script to the Camera because I want to display the time on the screen with GUI. At the same time, I call an AudioSource (to get the time of the music) so I also linked the audiosource to the camera.
Lastly, when the time hits a certain point, I want to play “Thunder”. It’s an anim that goes from transparant to white with sounds. I don’t think I add another audioSource to my camera, so my idea was to add the audio source to the UI layer and call both the animation and the audio when I’d like to.
Here is the code I have so far:
public class Time_Music : MonoBehaviour {
AudioSource audio;
int time;
int min;
int seconds;
bool thunder = false;
void Start () {
audio = GetComponent<AudioSource>();
}
void Update () {
time = 277 - (int) audio.time;
min = time / 60;
seconds = time - (min * 60);
if (min == 3 && seconds == 57)
{
Play Thunder anim + sound
}
}
private void OnGUI()
{
GUI.Label(new Rect(1500,50,1100,1100), "Halsey - Castle");
GUI.Label(new Rect(1530, 70, 1100, 1100), min.ToString());
GUI.Label(new Rect(1540, 70, 1100, 1100), ":");
if (seconds >= 10)
GUI.Label(new Rect(1545, 70, 1100, 1100), seconds.ToString());
else
{
GUI.Label(new Rect(1545, 70, 1100, 1100), "0");
GUI.Label(new Rect(1553, 70, 1100, 1100), seconds.ToString());
}
}
}
I sadly don’t know what do to in the “if” statement. I setted up a bool variable for the animation. I would like to say “if” conditions, then setbool → true, and that will start the script of “thunder”. Sadly, I don’t know how to make unity understand I refer to this variable if I didn’t call it.
I’ve read few things about Coroutine, but as the “if” statement is in Update, I cannot start the coroutine right away.
Finaly, I thought about creating a variable that will lead to the coroutine. However, I don’t know what behaviour should have the IENumerator function I will call for the coroutine…
I am quite lost with all the documentation I’ve read and few tutorials that I’ve seen. I think I would be able to do it if it was a collision(IENumerator OnCollisionEnter()), but as is it timers… I don’t know how to proceed.
Thank you very much for your help !
Here is the script i have for thunder:
public class Thunder : MonoBehaviour {
Animator anim;
bool play = false;
AudioSource audio;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
audio = GetComponent<AudioSource>();
}
public IEnumerator PlayThunder()
{
play = true;
anim.SetBool("play",true);
audio.Play(2);
while (play)
yield return null;
}
void AnimationComplete()
{
anim.SetBool("play",false);
play = false;
}
PS: I’d like to call Thunder many times. The first one will be with the music. The second one will be when the character arrives at a certain place (so collision will work well here). I’d like to make sure my thunder script works well in both situation.
LOOOOVE <3