- Home /
Yield causing 2D animation to pause
I'm having trouble getting my 2d animation to play properly. I have a character that has an idle animation, I've made it so that when you click on the character the animation changes to something else. I want it to play all of the frames of the animation (when clicked) and then return to it's idle state when the animation is finished, however i've tried using a yield WaitForSeconds at the length of the animation but this stops it from playing as though it's been paused, which I guess it kinda has. it freezes on the frame that the yield became active. I'm working with the animation code from the walker boys 2d mario tutorial which calls the frames in sequence in the code. I understand that Yield basically stops the code and thus stops my animation from playing the next frame. So i'm wondering if there is another way that I can wait a few seconds for my animation to finish before I switch back to the idle state.
Cheers.
Here's the code that I'm working with:
if (idle) {
aniPlay.aniSprite ( 8, 8, 0, 0, 16, 24 );
}
if (clicked){
aniPlay.aniSprite ( 8, 8, 0, 4, 16, 24 );
yield WaitForSeconds ( 5);
clicked = false;
idle = true;
}
if (Input.GetMouseButtonDown(0) ) {
if(Physics.Raycast(ray,hit,100.0)) {
if(hit.transform.tag == "character"){
idle = false;
clicked = true;
}
}
Answer by pheash · Jun 26, 2012 at 10:58 PM
heya, you could simply introduce a timer. store the game time into a variable when clicked turns to true and then have a conditional that checks if the game time is bigger than the stored time + an offset. eg:
var timeClicked;
// this goes into your raycast bit:
timeClicked = Time.time;
// this goes into your update function inside the if(clicked)
if(Time.time > timeClicked+5.0){clicked = false; idle = true;} // instead of using 5.0 create a variable waitTime and set it to the length of your animation..
hope that helps a bit.
np. could you mark the question as answered to keep the forums clean?