Hello Guest

Author Topic: Problems with getting animation to play using Input.GetAxis()  (Read 4467 times)

Gnoob

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
I have a problem with getting the animation to play using Input.GetAxis("Horizontal").
I want the animation to play the walking animation while the user still has right or left pressed.
here is my current implementation and its not working. The animation just stops and it moves forward and will continue playing after i stop pressing the button.

void Update()
    {
        transH = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
        if (transH > 0.0f)
        {
            transform.Translate(transH, 0, 0);
            anim.Play("walk_right");
            anim.animationCompleteDelegate = null;
        }
        if (transH < 0.0f)
        {
            transform.Translate(transH, 0, 0);
            anim.Play("walk_left");
            anim.animationCompleteDelegate = null;
        }
    }

please help. thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Problems with getting animation to play using Input.GetAxis()
« Reply #1 on: June 10, 2012, 11:03:55 pm »
You should only play the animation if its not playing. Calling play repeatedly, as your code will do, will make it get "stuck" on the first frame. Something like this will do the job -


Code: [Select]
if (anim.IsPlaying() && anim.CurrentClip.name != "walk_right")
    anim.Play("walk_right");

Gnoob

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problems with getting animation to play using Input.GetAxis()
« Reply #2 on: June 14, 2012, 01:57:07 am »
thanks that really helped!