Hello Guest

Author Topic: Walking Animation Then Idle Problems  (Read 4726 times)

nicntj

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Walking Animation Then Idle Problems
« on: May 26, 2013, 04:13:35 pm »
I have the C# code that makes the Animation move (from scripting an animated sprite in the documentation). And I did some modifications to it so when I walk left (a-key) the animation will play and when you take your finger of the key it will go to idle. But I can't get the same thing to happen when I'm walking right (d-key). My right animation doesn't even play it stays at idle. I haven't found much on this so thanks for your time and help.

here is my code:
Code: [Select]

public class TutorialAnimController : MonoBehaviour {

    // Link to the animated sprite
    private tk2dAnimatedSprite anim;

    // State variable to see if the character is walking.
    private bool walking = false;

    // Use this for initialization
    void Start () {
        // This script must be attached to the sprite to work.
        anim = GetComponent<tk2dAnimatedSprite>();
    }

    // This is called once the hit animation has compelted playing
    // It returns to playing whatever animation was active before hit
    // was playing.
    void HitCompleteDelegate(tk2dAnimatedSprite sprite, int clipId) {
        if (walking) {
            anim.Play("walkRight");
       
}
        else {
            anim.Play("idle");
        }
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKey(KeyCode.A)) {
            // Only play the clip if it is not already playing.
            // Calling play will restart the clip if it is already playing.
            if (!anim.IsPlaying("walkLeft")) {
                anim.Play("walkLeft");

                // The delegate is used here to return to the previously
                // playing clip after the "hit" animation is done playing.
                anim.animationCompleteDelegate = HitCompleteDelegate;
            }
        } else {

            if (!anim.IsPlaying("idle")) {
                anim.Play("idle");
                anim.animationCompleteDelegate = null;
                walking = false;
           
        }
}
     if (Input.GetKey(KeyCode.D)) {
            if (!anim.IsPlaying("walkRight")) {

                // Walk is a looping animation
                // A looping animation never completes...
                anim.Play("walkRight");

                // We dont have any reason for detecting when it completes
                anim.animationCompleteDelegate = null;
                walking = true;
            }
        }
 
}

}


dpk

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Walking Animation Then Idle Problems
« Reply #1 on: May 26, 2013, 06:40:41 pm »
You've got your anim.Play("idle") code set up as an else only to your test for the a-key input. I think some of the whitespace got lost in the paste (not sure though), but basically here's what I got when I re-indented the code:

Code: [Select]
  void Update () {
    if (Input.GetKey(KeyCode.A)) {
      // Only play the clip if it is not already playing.
      // Calling play will restart the clip if it is already playing.
      if (!anim.IsPlaying("walkLeft")) {
        anim.Play("walkLeft");

        // The delegate is used here to return to the previously
        // playing clip after the "hit" animation is done playing.
        anim.animationCompleteDelegate = HitCompleteDelegate;
      }
    } else {
      if (!anim.IsPlaying("idle")) {
        anim.Play("idle");
        anim.animationCompleteDelegate = null;
        walking = false;
      }   
    }
    if (Input.GetKey(KeyCode.D)) {
      if (!anim.IsPlaying("walkRight")) {
        // Walk is a looping animation
        // A looping animation never completes...
        anim.Play("walkRight");

        // We dont have any reason for detecting when it completes
        anim.animationCompleteDelegate = null;
        walking = true;
      }
    }
  }

I think this makes it more clear that the idle code is only called in one condition. While this would not be the ideal end solution, if you moved your if Input.GetKey(D) up a bit, you'd probably get the result you want for now, like so:

Code: [Select]
  // Update is called once per frame
  void Update () {
    if (Input.GetKey(KeyCode.A)) {
      // Only play the clip if it is not already playing.
      // Calling play will restart the clip if it is already playing.
      if (!anim.IsPlaying("walkLeft")) {
        anim.Play("walkLeft");

        // The delegate is used here to return to the previously
        // playing clip after the "hit" animation is done playing.
        anim.animationCompleteDelegate = HitCompleteDelegate;
      }
    } else if (Input.GetKey(KeyCode.D)) {
      if (!anim.IsPlaying("walkRight")) {
        // Walk is a looping animation
        // A looping animation never completes...
        anim.Play("walkRight");

        // We dont have any reason for detecting when it completes
        anim.animationCompleteDelegate = null;
        walking = true;
      }
    } else {
      if (!anim.IsPlaying("idle")) {
        anim.Play("idle");
        anim.animationCompleteDelegate = null;
        walking = false;
      }   
    }
  }

At some point you'll probably want to change this entirely, so that the walking animation is based on the character's actual movement (based on the speed, direction, change-of-direction, etc) but eh. This'll work for now.

nicntj

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Walking Animation Then Idle Problems
« Reply #2 on: May 26, 2013, 07:14:08 pm »
This is really helpful thanks a lot!

But I had an idea once it started working. What I would like it to do is lets say your walking on the right side then stop the idle image needs to be facing the right. Then if your walking left when you stop the idle image needs to be facing left. I have a leftIdle and a rightIdle animation how do I program it in. Is it just another else if or else conditional statement?

Thanks a ton!