Hello Guest

Author Topic: How to get the player to go straight to idle after letting go of the button?  (Read 3587 times)

zaccom

  • Newbie
  • *
  • Posts: 1
    • View Profile
Here the general code from documents.

 using UnityEngine;
using System.Collections;

public class TutorialAnimController : MonoBehaviour

{
virtual public float x{get; protected set;}
   
   /// <summary>
   /// The y movement. 1.0f = up, -1.0f = down.
   /// </summary>
   virtual public float y{get; protected set;}
   
   /// <summary>
   /// Is the jump button being held down.
   /// </summary>
   virtual public bool jumpButtonHeld{get; protected set;}
   
   /// <summary>
   /// Was the jump button pressed this frame.
   /// </summary>
   virtual public bool jumpButtonDown{get; protected set;}   
   // 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 WalkCompleteDelegate(tk2dAnimatedSprite sprite, int clipId) {
      if (walking) {
         anim.Play("walk");
      }
      else {
         anim.Play("idle");
      }
   }


   
   // Update is called once per frame
   void Update () {
   //   jumpButtonHeld = false;
   //   jumpButtonDown = false;
      x = 0;
      y = 0;
      
      if (Input.GetKey(KeyCode.Space)) {
         // Only play the clip if it is not already playing.
         // Calling play will restart the clip if it is already playing.
         if (!anim.IsPlaying("jump")) {
            anim.Play("jump");

            // The delegate is used here to return to the previously
            // playing clip after the "hit" animation is done playing.
      //      anim.animationCompleteDelegate = HitCompleteDelegate;
         }
      }
      
      if (Input.GetKey(KeyCode.D)) {
         if (!anim.IsPlaying("walk")) {
            
            // Walk is a looping animation
            // A looping animation never completes...
            anim.Play("walk");
         
            
            // We dont have any reason for detecting when it completes
            anim.animationCompleteDelegate = null;
            walking = true;
         
      
 
}

         
   }   
   if (Input.GetKey(KeyCode.A)) {
         if (!anim.IsPlaying("walk-l")) {
            
            // Walk is a looping animation
            // A looping animation never completes...
            anim.Play("walk-l");

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



Afro-Ninja

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 62
    • View Profile
psuedo-code

Code: [Select]
if( Input.GetKey(KeyCode.D))
{
//walk right
}
else if(Input.GetKey(KeyCode.A))
{
//walk left
}
else
{
//return to idle
}

your script is close but you need to check these three conditions together. ALSO, you need to make sure the character is not airborne. if they are, the character will simply be falling and not in a walking OR idle animation

lastly this doesn't really have that much to do with toolkit 2d, this is general unity/c# programming