- Home /
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
Answer by Catlard · Jun 04, 2011 at 01:19 PM
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:
http://unity3d.com/support/documentation/ScriptReference/Animation.Play.html
However, it will play automatically unless you split the animation and give it a name.
Helpful, hopefully?
Cheers.
The script which you have provided, does not do anything i have placed it on the object which is supposed to wave if this is what i am supposed to do, but nothing changes. i did everything else you said but that didnt seem to change anything either.
Thanks for posting though
Well, you have to change "wave" to whatever name your animation has. Does that make sense?
What do you mean change wave, my animation is called Wave
I don't know, but it can't be good! Name it Fisheggs or something else.
Why don't you show me your code?
// Plays the Wave animation - stops all other animations in the same layer animation.Play("Wave"); // Plays the Wave animation - stops all other animations animation.Play("Wave", Play$$anonymous$$ode.StopAll);
Answer by Shrimpy · Jun 04, 2011 at 03:13 PM
This is my script
// Plays the Wave animation - stops all other animations in the same layer
animation.Play("Wave");
// Plays the Wave animation - stops all other animations
animation.Play("Wave",PlayMode.StopAll);
Answer by alex2018 · Nov 27, 2014 at 05:27 PM
hellow i have a code for activate animation with a press key
void Update () {
if(Input.GetKeyDown("h"))
animation.Play ();
}
Thank you! I just spent two hours trying to get this to work and was so frustrated. Can you tell me how I would add on to this to target a specific animation rather than the only one I have present? Cheers!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Animation script with is.grounded help 0 Answers
change animation name in inspector 2 Answers
Animation component in Unity 4.3 doesn't work? 2 Answers
Animation Problems 1 Answer