can someone explain this 1 line of code? didn't understand documentation

I found these codes

yield WaitForEndOfFrame();

yield WaitForSeconds (animation.clip.length);

but how do they work? for example I want to make it so that after the animation has ended do something.

so if I type

function example (){
animation.play(“animation 1”);
yield WaitForEndOfFrame();
debug.log(“animation 1 has finished”)
}

Would this code read the debug.log after the animation? or do I have to type
yield WaitForEndOfFrame(“animation 1”);
?

edit:
I also found this piece of code, but it doesn’t work, I was told that it should. can someone give me some advice?

if(animation[“animation 1”].time>=animation[“animation 1”].length){
debug.log(“animation 1 has finished”)
}

So if I’m understanding you correctly, you want something to stop until after an animation completes playing. Correct? What is the animation? What are you trying to have yeild? Can we get a few more details then we can probably help.

Im making a hack and slash game so as part of my combat I’ve made something like this

function attack (){
if (input.key.keycode(Z)){
attack 1 = true;
attackdamage = 50;
}
}

function attack animation ()
{
if(attack 1){
animation.play(“attack 1”)
yield WaitForEndOfFrame();
attack 1 = false;
attackdamage = 0;
}

I mainly want the animation to play once when the key is pressed not held down, and I want some variables to be turned on while hes attacking then turned of when not.
for instance while attack animation is playing, player cannot move character, and if a certain command is pressed while in that state, the player can perform a combo. but if the user waits till after the animation, the user regains control but cannot perform the combo.

Sorry Raybrand, thats over my head me. I leave the complex stuff to the programmers at the office :slight_smile:
First thing you could try looking up is double jumps. If I recall there are a few threads where people talk about doing double jump code. The only reason I suggest that is double jump has some similar principals to what you want to do. If jumping and you hit the key again you double jump, when can you double jump, when can’t you. That sorta thing. Just replace “jump” with “attack” and you might have a starting point.

Otherwise, If I were doing this I would start out as simple as possible. Make your your avatar play the attack animation when you press Z. Then make it so you can’t spam the button but it has to play through the entire animation before allowing you to do it again, and so on.

Sorry I couldn’t be of greater help, your floating above my skills.

You need to enable a flag on keydown, disable it on keyup, and in keydown only play the animation if the flag is disabled.

Hey man!

yield WaitForEndOfFrame() does not do what you’re thinking it does. Instead, this type of yield will wait until all the cameras and GUI stuff has been rendered, and then the code after it will fire. It seems to be mostly for capturing the current view and saving it as image data.

What you want to do is actually really simple. Try this:

function Attack(){
  animation.Play();
  yield WaitForSeconds (animation.clip.length);
  //anything you want applied after the animation finishes
}

This will play the animation and then wait to fire any code following the yield call. Good luck!

it didn’t work, Rapskailain, when I use it applies the effect half way through the animation rather than at the end.

please keep sending more suggestions

The way I always do it when I need to wait till an animation is finished is like this

While(animation.IsPlaying("Attack")){
yield;
}
//Stuff when animation ends

afraid it didn’t work, but it sounds like it should so I think the flaw may lie in the rest of my code.

function Attack()
if(Input.GetKey(KeyCode.Z)){
Attacking = true;
}

function AttackAnimation(){
if(Attacking){
animation.Play("Blizzard Edge", PlayMode.StopAll);
While(animation.IsPlaying("Attack")){
yield;
}
Attacking = false;
}
}

What I’m aiming for at the moment is, user presses the Z button to tell the script the player is attacking, so in the AttackAnimation function it reads the boolean so that the user doesn’t have to hold down the key. Then when the animation has finished turn the boolean to false so it doesn’t play the animation.

Sorry I guess I didn’t explain the code well enough. Inside the “animation.IsPlaying” part it should be the name of the animation your playing. So in this case it would be

while(animation.IsPlaying("Blizzard Edge")){
yield;
}

oops no thats not it when I typed the code on the computer I miss typed it, so I wrote the code as shown below.

function Attack()

if(Input.GetKey(KeyCode.Z)){

Attacking = true;

}

function AttackAnimation(){

if(Attacking){

animation.Play(“Blizzard Edge”, PlayMode.StopAll);

While(animation.IsPlaying(“Blizzard Edge”)){

yield;

}

Attacking = false;

}

}

whats happens is that it plays the first frame then ends the animation. tried yield wait for seconds but it cuts the animation half way. and yield end of frame does nothing.

it just plays the animation in a loop. If I use yield wait for seconds and put the attack = false in the while loop it waits turns attack to false half way through the animation, with the other yield commands they do nothing.

Try this. You might need to specify exactly which animation’s length to use. ‘clip’ just gives you the default animation.

function Attack(){
  animation.Play("Blizzard Edge",PlayMode.StopAll);
  yield WaitForSeconds (animation["Blizzard Edge"].length);
  //anything you want applied after the animation finishes
}

I typed it in but Im getting weird results, sometimes my character does what I want and ends the attack when I want, other times it plays the attack again twice after pressing it once but on the second time it ends shortly. The rest it plays and cancels half of the animation.

any reason as to why it does that?

how come this line of code doesn’t work
if(animation[“animation 1”].time>=animation[“animation 1”].length){
debug.log(“animation 1 has finished”)
}

anyway I can edit it to make it work.

how do I make it so that the character plays the animation just once? the thing is that the function is called every frame so it keeps playing the animation over and over

You are calling Attack() every frame? Or doing that debug check every frame? Sorry I’m not sure what you meant.

As far as making sure the animation only plays once there is this:

animation.wrapMode = WrapMode.Once;

//or maybe try rewinding the animation once its done
animation.rewind("Blizzard Edge");

Animation doesn’t have a ‘time’ variable from what I can see. You might find use of the ‘animation.isPlaying()’ method though for debugging purposes.

EDIT: Oh I see the ‘time’ variable now. Maybe its not able to log that because the time is being wrapped immediately back to 0. Just a guess.

thnx will try, what I meant by being called each frame is, there is a function called attack and attackanimation which is in the function update. Reason for this is that I have to check each frame when the attack key is pressed, the attackanimation fuction needs to be checked each frame to see what attack is being played.

So if the attackanimation function is bieng called each frame then the animation keeps playing each frame. Thats why I wanted a way of detecting when the animation has finished to turn the boolean off.

function AttackAnimation(){
if (Attack){
animation.wrapMode = WrapMode.Once;
animation.play(“attack”);
}
}

this function is being called each frame so If I put /animation.wrapMode = WrapMode.Once;\ will it still play only once? Also I wanted it so that when the button is pressed it calls this function, but I found out that I have to keep the button held down otherwise the animation gets cut off.