animation when a button is pressed

hiya.

i’m pretty new at Unity and i have this thing that i just couldn’t figure out myself: how do you script a button, so that when it’s pressed, it’ll show an (pre-made) animation? (so if you press ‘forward’ a walking animation starts and when you release the button, the animation’ll stop playing) and is it possible to script this inside of the FPSwalker script?

thanks,

  • NgInE

Do you mean a gui button or a button on the keyboard?

a button on the keyboard.

can anyone tell me please? :slight_smile:

function Update ()
{
 if (Input.GetKey ("up"))
 {
     if (!animation.isPlaying)
       animation.Play();
 }
 else
 {
       animation.Stop();
 }
}

a wow, thank you!

oh, and should i use this one in the FPS script? (sorry for all the dumb questions, but i’m a noob when i comes to scripting.)

The script is applied to the object it’s attached to - and the fps script is attached to a capsule which you can’t see. If you want that capsule to be animated, go ahead :slight_smile:

I’d recommend putting it all in a separate script and applying that script to the object you want animated.

thanks it works great :smile: is it also possible that, when you press ‘down’, and thus the character moves backwards, the animation also plays in reverse?