So, I am fairly new to scripting, and I am having troubles with a sequence that I am trying to set up… ideally, it goes like this:
when you press “f”, the sequence starts, with an animation (looped) and a sound playing
While these are playing, if you press “f” again, this does not affect the sequence
when the sound finishes, the animation stops with it, and another, shorter sound plays
after all this, if you press “f” again, it can start all over again
So, I’ve tried various approaches to these stages, and I’m encountering various problems…
the end of the sound does not trigger what I want, or anything at all really
when i’ve tried to stop the animation using a WaitForSeconds, it doesnt listen to it but ends the animation immidiately, doesnt wait at all
whenever “f” is pressed again during the sequence, you can hear another instance of it start up in the background… its really annoying, if you keep pressing the key, it generates this huge bluster of noise from all the sequences happening at once
The code generating all this hullabaloo is below:
var During : AudioClip;
var After : AudioClip;
function Update (){
if (Input.GetKeyDown("f")){
animation.Play("Flight");
audio.PlayOneShot(During);
if (!audio.isPlaying == false){
Debug.Log("Are you done yet?");
audio.PlayOneShot(After);
}
//WaitForSeconds(22);
//animation.Stop("Flight");
//audio.PlayOneShot(After);
}
}
And there you have it. As you can see, the “WaitForSeconds” thing is a path I chose not to pursue but left there just in case it can be used…
If anyone could help my rather frustrating problems, I would be very grateful… Some of it is probably silly mistakes, but I will learn and know for next time
Alright, let’s see if we can tackle these problems one by one.
The end of the sound does not trigger what I want, or anything at all really
This is to be expected. Right now, your if statement for the audio (if (!audio.isPlaying == false)) is nested within the if statement above it (if (Input.GetKeyDown(“f”))). The only way that the end of the sound will trigger anything is if you press the f key at the EXACT frame that the audio stops…which is darn near impossible. You’re going to want to unnest those if statements for that to work properly. I think I see what you were trying to do though. Try this out.
var began : boolean = false
function Update (){
if (Input.GetKeyDown("f")){
animation.Play("Flight");
audio.PlayOneShot(During);
began = true;
}
if (!audio.isPlaying && began){
Debug.Log("Are you done yet?");
audio.PlayOneShot(After);
began = false;
}
}
Whenever “f” is pressed again during the sequence, you can hear another instance of it start up in the background… its really annoying, if you keep pressing the key, it generates this huge bluster of noise from all the sequences happening at once
With the code we added in our previous step, this could be an easy fix. Replace
if (Input.GetKeyDown("f")){
with…
if (Input.GetKeyDown("f") && !began){
This will only fire when both conditions are met (The button is pressed AND the animation hasn’t already begun).
When i’ve tried to stop the animation using a WaitForSeconds, it doesnt listen to it but ends the animation immidiately, doesnt wait at all
This one is going to be a little tricky to answer, but I’ll give it a shot. Whenever you want to “WaitForSeconds(x)”, you want to add a yield statement before it. Otherwise, all you’re doing is telling the CPU to count to x, but it’s going to keep doing everything else that it’s doing while counting. Yield actually makes it stop what it’s doing while it counts to x. Now, having said that, you can’t have any yield statements in the Update() function. Update never yields; it’s called every frame. What you can do is, instead of having everything written directly into the Update function, do something like this.
function Update () {
CheckInput();
}
function CheckInput() {
//Your code with the yield WaitForSeconds(22); in there somewhere.
}
It was a pretty long answer, and I hope I haven’t made any mistakes in it, but it should definitely get you going in the right direction. Good luck!