2D Toolkit Forum

2D Toolkit => Support => Topic started by: Gnoob on June 10, 2012, 09:38:23 pm

Title: Problems with getting animation to play using Input.GetAxis()
Post by: Gnoob on June 10, 2012, 09:38:23 pm
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
Title: Re: Problems with getting animation to play using Input.GetAxis()
Post by: unikronsoftware 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");
Title: Re: Problems with getting animation to play using Input.GetAxis()
Post by: Gnoob on June 14, 2012, 01:57:07 am
thanks that really helped!