Hello Guest

Author Topic: Animation stuck on first frame - how to stop this??  (Read 7115 times)

bluelotus

  • Newbie
  • *
  • Posts: 18
    • View Profile
Animation stuck on first frame - how to stop this??
« on: October 06, 2012, 11:55:45 am »
Hi Guys,

Just got the 2dtoolkit and its good. :D

However, I am new to unity coding and am having trouble making the animation play correctly.

The following confuses me.
It says that the CurrentClip name is "walkRight" -
If this is true it should not re-trigger the animation -
But it seems to be re-triggering the animation every frame anyway?

I must be missing something, (perhaps to do with the delgate, which I have bypassed)
Any advice would be appreciated -

Here is the snippet,

Debug.Log( "current anim " + anim.CurrentClip.name);
      if (anim.CurrentClip.name != "walkRight")  {
    anim.Play("walkRight");}


And here is the full code -

   void Update ()
   {
      
         Debug.Log( "PLaying" + anim.Playing);
      
      
      if ( Xdest > transform.position.x )
      {
         
            Debug.Log( "current anim " + anim.CurrentClip.name);
         if (anim.CurrentClip.name != "walkRight")  {
    anim.Play("walkRight");}
         
         //anim.Play("walkRight");
         //anim.animationCompleteDelegate = null;
         transform.Translate(new Vector3(1,0,0));
         walkingRight = true;
      
         
      }
      
      else if ( Xdest < transform.position.x)
      {   
         if (anim.CurrentClip.name != "walkLeft")  {
    anim.Play("walkLeft");}
         
         
         //anim.Play("walkLeft");
         //anim.animationCompleteDelegate = null;
         transform.Translate(new Vector3(-1,0,0));
         walkingLeft = true;
         Debug.Log( "current anim " + anim.CurrentClip.name);
      }
      
      else
      {
         anim.Play("idle");
         anim.animationCompleteDelegate = null;
      }
   }
}
   
Thanks,

BLueLotus.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Animation stuck on first frame - how to stop this??
« Reply #1 on: October 06, 2012, 03:04:19 pm »
Firstly, CurrentClip can be "not null" when an animation has stopped playing, so you should check that something IS playing and that the currentClip == name. The play state is managed independently to the clip.

For this specific issue though, you should check that the clip name is exactly the same and doesn't have any invisible spaces in it... that would certainly throw off the name comparison... stop this in the debugger and you should be able to compare them to find out why anim.Play is being called again.

bluelotus

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Animation stuck on first frame - how to stop this??
« Reply #2 on: October 07, 2012, 03:47:44 am »
Right so the debug info looked correct -
I changed to clipId to remove possible naming issues.

   if ( Xdest > transform.position.x )
      {
         if (anim.Playing == true && anim.clipId != 1)  {
             anim.Play("walkRight");
         }

                transform.Translate(new Vector3(1,0,0));
                }


It still didn't work.
Back to Unity and it turns out my frames in the Tk 2d animation script were all set to a length of 1 frame. 
Not sure how this was reset prob due to Unity crashing with me not saving often enough.

Anyway it works with the frames set to a longer length like 3.
Bit of a noob mistake.  8)

ALL GOOD!

BlueLotus.