• 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
Question by Shiro_Rin · Oct 18, 2015 at 12:20 AM · animation2dinputanimatorgetbutton

GetButton will only play animation when held down

How do I play an animation completely with Input.GetButton ? It'll only play the complete animation if the button is held down.

 if (Input.GetButton ("Attack1")&& grounded==true && IsMoving==false) {
             Anim.Play("Attack1");
             IsAttacking=true;
         }

That is the snippet of code I am using for attacking. So far it works, but only if I hold down the button. I want it to play completely tho. Any help ?

Comment

People who like this

0 Show 6
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 PictonicStudio · Oct 18, 2015 at 03:26 AM 0
Share

Why not use Mecanim? It's much simpler

avatar image Shiro_Rin · Oct 18, 2015 at 03:29 AM -1
Share

I am using mecanim. It's 2D sprite. Anim is a reference to an Animator component. I fixed it my own way. I use coroutine's to detect if an attack is going and it plays the animation.

avatar image PictonicStudio Shiro_Rin · Oct 18, 2015 at 04:02 AM 0
Share

But, you should use Anim.SetBool/Float/Int

avatar image TrickyHandz · Oct 18, 2015 at 04:33 AM 0
Share

I see you fixed this your own way, but for reference you should look at how you are handling input:

 // This will play the animation while the button is being held down 
 if (Input.GetButton ("Attack1")&& grounded==true && IsMoving==false) {
              Anim.Play("Attack1");
              IsAttacking=true;
          }
 
 // This will play the animation when the button is pressed
  if (Input.GetButtonDown ("Attack1")&& grounded==true && IsMoving==false) {
              Anim.Play("Attack1");
              IsAttacking=true;
          }
 
 // This will play the animtion when the button is released
  if (Input.GetButtonUp ("Attack1")&& grounded==true && IsMoving==false) {
              Anim.Play("Attack1");
              IsAttacking=true;
          }

avatar image Shiro_Rin TrickyHandz · Oct 18, 2015 at 04:36 AM 0
Share

I tried GetButtonUp , and it did the same thing.

avatar image TrickyHandz Shiro_Rin · Oct 18, 2015 at 04:42 AM 0
Share

In that case, the problem may exist in the Mecanim state machine. If a state has no exit time (i.e. looping animations) and the state can be interrupted, it will leave in the middle of the animation depending on the conditions. This is one of the reasons that most people use parameters to drive the states and transitions rather than calling Animator.Play() to jump directly to a state.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Shiro_Rin · Oct 18, 2015 at 03:30 AM

I fixed it using coroutine's. My own way I guess

Comment
PictonicStudio
DireDoesGames

People who like this

0 Show 0 · 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

Answer by DireDoesGames · Oct 18, 2015 at 09:13 AM

You might be able to put the Anim.Play("Attack1"); in the IsAttacking variable.


If my answer helps you please accept it and up vote it! Thanks!

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

46 People are following this question.

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

Related Questions

2D Animation does not start 1 Answer

Animation for dismembered limbs keeps playing 0 Answers

how to make animation wait for another animation 1 Answer

Animations not carrying over scene to scene 1 Answer

How can a 2D Sprite remain on the same space when the sprite is stretched? 2 Answers


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