1
\$\begingroup\$

Alright, I have posted to Unity as well and watched tutorials but I am unable to make a walk animation work code wise. I have a complex problem being that in my game the user taps to move, thus the character is either idling, walking just one step at a time or walking continuously. So far I transition from idle to walking (in just individual steps, meaning the user isn't tapping repeatedly) with this setup and this code, and it works well -

I have an idle state and the walking clip, and the transition between the 2 is a bool "isWalking" that is either true or false.

I cannot merely trigger the walk animation without an offset because I have to alternate between left and right steps, so I use an increasing offset that (I think I did it right) should move through the whole animation, depending on if the object is moving:

     if (animOffset >= 1f) { //was full clip
                 animOffset = 0.0f;
             }

             //anim help
             currentPos = transform.position;

         if (currentPos != lastPos) {
             //print ("moving now");
             animator.SetBool ("isWalking", true);
             if (InputManager.stepCount % 2 != 0) {
                 print ("moving right");
                 animator.SetFloat ("walkOffset",animOffset);

             } else {
                 print ("moving left");
                 animator.SetFloat ("walkOffset",animOffset);

             }
             animOffset += 0.1f;
         } else {
             animator.SetBool ("isWalking", false);
         }
         lastPos = currentPos;

When the user taps repeatedly however, the animation SHOULD not start over again from its offset in individual steps - it should just play the animation for the duration of its movement. Problem is using this if I watch the blue bar on the animation states, it ALWAYS starts from the beginning if I keep a consecutive step count and set the offset to 0.0.

With just this, the walking is very glitchy if its not in individual steps as its starting again and again. Im new to animation - how can I do this?

How can I make sure an animation plays CONTINUOUSLY if the object is moving without stopping for too long? I have no idea how to do this

\$\endgroup\$
  • \$\begingroup\$ Try to create another arrow from "Any State" to your walking state in the animator window, and make it work when the user taps. And make sure that only if the user doesn't tap before the end of the walking animation it goes to idle. \$\endgroup\$ – Vadim Tatarnikov Dec 20 '16 at 16:36
1
\$\begingroup\$

Your problem is with the inputs and how you're handling the movement system. You're checking if the position has changed or not, and in case of Unity, the position had changed in that frame where you pressed the button, but did not change in the frame after it where you didn't press the button.

Let's put a timer in there shall we? First, define this float outsite the update code (where you define other variables): float timerFloat = 3;

if (currentPos != lastPos) {
        timerFloat=1;
      //print ("moving now");
        animator.SetBool ("isWalking", true);
if (InputManager.stepCount % 2 != 0) {
        print ("moving right");
        animator.SetFloat ("walkOffset",animOffset);
} else {
        print ("moving left");
        animator.SetFloat ("walkOffset",animOffset);

}
        animOffset += 0.1f;
} else {
        if(timerFloat <= 0){animator.SetBool ("isWalking", false);}
        timerFloat = timerFloat - Time.deltaTime;
}
lastPos = currentPos;

Now your character should still be "moving" for one second after the key press stops. (Change the values or play around with timers for "smoother" movement animation. Try not to include timers in movements themselves and only animations as this might mean taking control away from the player, which will make your game feel unresponsive.)

\$\endgroup\$
  • \$\begingroup\$ No, that did not work. This just makes it play the walk cycle forever. Check my most recent question I use timers but they don't workhttp://stackoverflow.com/questions/41272984/how-distinguish-between-consecutive-tapping-and-single-taps-unity-animation/41273757#41273757 \$\endgroup\$ – skyguy Dec 22 '16 at 0:40
0
\$\begingroup\$

If you select the animation file in Unity's project explorer you should find a loop setting for the animation in the project inspector.

I had this same problem a while ago, no matter what I did in the code the animation would not loop until I set the animation settings up directly in the inspector.

\$\endgroup\$
  • \$\begingroup\$ No, thats not the problem. I can make it loop - the animation just always plays from the BEGINNING or starts over, not playing continuously \$\endgroup\$ – skyguy Dec 20 '16 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.