x


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;
       }        

}

more ▼

asked Jun 26 '12 at 10:52 PM

reltuc gravatar image

reltuc
0 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jun 26 '12 at 10:58 PM

pheash gravatar image

pheash
352 1 5 9

That's great, just what I needed. Thanks a lot Pheash!

Jun 27 '12 at 08:31 PM reltuc

np. could you mark the question as answered to keep the forums clean?

Jul 24 '12 at 09:52 AM pheash
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x335
x170
x8

asked: Jun 26 '12 at 10:52 PM

Seen: 381 times

Last Updated: Jul 24 '12 at 09:52 AM