Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by TheMinecrafter31 · Dec 07, 2014 at 11:07 PM · gameanimationsplay

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!!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheMinecrafter31 · Dec 15, 2014 at 10:07 PM 0
Share

Thx this helped me alot and thanks for taking your time to explain all of that, not many people help others like you did :)

avatar image Hronet · Dec 15, 2014 at 10:14 PM 0
Share

Hey no problem! I just hope my solution worked for you!

avatar image xXM7MDXx Hronet · Mar 18, 2016 at 01:57 PM 0
Share

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

avatar image Hronet xXM7MDXx · Mar 18, 2016 at 04:44 PM 2
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by unity_Anjt0b7JlcajdQ · Oct 10, 2018 at 01:40 AM

whats mytrigger?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Game Movement script(W,A,S,D input only)? 1 Answer

each enemy have different damage amount, how to calculate the hp remain??? 1 Answer

Error... that I don't understand. 3 Answers

Is it good to have many scripts? 1 Answer

A node in a childnode? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges