Animation On Button Press

Hi all, what i am trying to do is play an animation when a button is pressed, for example when the space-bar is pressed it plays a waving animation. I have already animated this in blender and imported it into unity but i dont know how i would play the animation when a certain button is pressed. I have very little scripting skills and all i know about this is it might have Get.ButtonDown in it, apart from that i am clueless, could anyone help me with this? Thank you

Well, you’ll have to set up the animation not to play by “default”. If you highlight the object that you’ve animated in the project, then look down towards the bottom where it asks you if you want to split animations. You click the box, and it will create an animation with a name (by default “idle”) and you’ll need to specify the number of frames in your animation. Then, you can go into the script attached to the object, the one you want to cause the animation to play, and you use the code on this script reference page:

However, it will play automatically unless you split the animation and give it a name.

Helpful, hopefully?


This is my script

// Plays the Wave animation - stops all other animations in the same layer
// Plays the Wave animation - stops all other animations

hellow i have a code for activate animation with a press key

void Update () {

 animation.Play ();