- Home /
How do i make an animation play on key press?
Hi guys this is my first post. I would very much like to know how i can make an animation play when i press a key. I started using unity about 3 days ago so talk to me like im stupid and with detail. i tried making a script and this is what i have:
function Update()
{
if(Input.GetKeyDown("w"))
{
// Plays the walking animation - stops all other animations
animation.Play("walking", PlayMode.StopAll);
}
}
im almost 100% sure that is correct but when i start the game the animation starts playing without me pressing anything. I have dragged the script to the object that i want to animate in the Hierarchy and i made sure the names are correct. I an also getting this error:
MissingComponentException: There is no 'Animation' attached to the "1FPS_HAND" game object, but a script is trying to access it. You probably need to add a Animation to the game object "1FPS_HAND". Or your script needs to check if the component is attached before using it. walkingscript.Update () (at Assets/Anims/Walking/walkingscript.js:6)
What does that mean??? how do i add an animation, i already have an animation. And i know this because when i select "1FPS_HAND" on the animation window, i can see the animation is there. But i dont see an animation section in the inspector panel but i see animator. Can anybody help me with this? Im pretty sure its not the script thats wrong. Let me know if you need more info. THANK YOU GUYS!!!
Answer by Hronet · Dec 15, 2014 at 10:04 PM
I'm going to assume you have an Animator attached to your object and it is taking the flag and running. I'm not even sure that the way I handle this is the best but I'll invite you to explore my solution to this problem.
Open up the Animator window. This can be found at the top under "Window" and "Animator" (I believe it has no hotkey associated with it.) When this opens, select your Game Object in your Hierarchy. If the Animator has an orange box with the name of your animation in it, this is why it is auto-animating. The way that I fix this is that I right click inside the Animator window and create a new state and I call it Idle. Inside Idle I don't assign any animations at all. Right click Idle and set it to the default state, which should then make it orange. From here you want to right click on Idle again to create a transition, at this point an arrow will appear underneath your mouse cursor and you will want to use it to click on your other animation state (the one that was originally orange.) This tells the Animator you want to go from Idle (where no animation is playing) to a state where an animation is playing. In the bottom left of your Animator window is a box called "Parameters", go ahead and click on the + on the rightside of that box and add yourself a Trigger which you can name anything you want. Then click on your transition arrow from Idle -> Whatever Animation and check the inspector. Change the Condition from "Exit Time" to your Trigger. You will also want to set a transition FROM your Animation TO Idle so that it exits after the animation is complete (and you can use the default settings for a transition for that, no new triggers required.)
After you do that block of text, go into your code and inside the check for input write GetComponent().SetTrigger( "NameOfYourTriggerYouMadeThatIDontKnowLol" );
... or whatever the uh Boo equivalent is? idk I'm a C# man :( I think you get the picture though.
Thx this helped me alot and thanks for taking your time to explain all of that, not many people help others like you did :)
hey... i want to know what do u mean by saying block of text, go into your code and inside the check for input write GetComponent().SetTrigger( "NameOfYourTriggerYou$$anonymous$$adeThatIDont$$anonymous$$nowLol" ); NOOB
By "Block of Text" I literally just mean all of the comments that I wrote in the paragraph before that haha. I kind of rambled in my explanation.
If I'm to assume that you're not a programmer and just want to know the quickest way to get an animation to play on key press, right click in your Project view and create a new C# Script. This can also be done by going to the top of your Unity window and clicking on "Assets" in the toolbar, then Create -> C# Script. This will generate a new script called "NewBehaviourScript". Go ahead and rename that to something like "AnimateOn$$anonymous$$eypress".
Open the script in $$anonymous$$onoDevelop (I am going to assume you do not have Visual Studio installed, this is fine. Unity comes with $$anonymous$$onoDevelop.) To do this simply double click the script in your Project window. It will come pre-built with two functions, Start() and Update(). Here, I'll just write a quick script you can copy paste in.
public $$anonymous$$eyCode $$anonymous$$y$$anonymous$$ey;
public string $$anonymous$$yTrigger;
void Update ()
{
if (Input.Get$$anonymous$$ey($$anonymous$$y$$anonymous$$ey))
{
GetComponent<Animator>().SetTrigger($$anonymous$$yTrigger);
}
}
Now exit $$anonymous$$onoDevelop and head back to Unity. Drag that script on to the Game Object inside your Hierarchy view that you would like to animate on key press. When you do, you will see your script inside the Inspector view. There will be two fields that you have to fill out here, one is a dropdown for what key you would like to begin the animation, and the other is the name of the Trigger that you setup in your Animator window (assu$$anonymous$$g you followed all the steps in my previous explanation.)
http://i.imgur.com/hLSrEPi.png Here is a link to a screenshot I took of what it should look like once it's all setup. I selected "$$anonymous$$" as my key and the name of my trigger in my Animator window is "BeginAnimation" so I set that as "$$anonymous$$y Trigger" in my script window.
Answer by static_cast · Dec 08, 2014 at 12:46 AM
Click on the 1FPS_HAND object and go to the top of your screen to "Component>Miscellaneous>Animation" and give it an animation clip.