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
0
Question by Pathojen · Aug 11, 2020 at 11:43 PM · uibuttonanimatorgetbuttondown

Animator not setting bool on GetButtonUp?

I'm trying to make an animation play when I press a UI button, and another one when I release it. It works for the GetButtonDown, but not GetButtonUp. Am I doing something wrong here? Thanks for help.

Here's the bit of code on that.

     void FixedUpdate()
     {
     if(Input.GetButtonDown("AttackButton"))
         {
         anim.SetBool(P_ATTACKING, true);
         }
 
     else if(!Input.GetButtonUp("AttackButton"))
     {
     anim.SetBool(P_ATTACKING, false);
     }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Aviryx · Aug 11, 2020 at 11:56 PM

So I originally thought you were trying to using a custom keybind but now see you actually have a UI button that is being pressed.


 public Button attackButton;

 void Start()
 {
     attackButton.OnClick.AddListener(StartAttack);
 }

 private void StartAttack()
 {
     anim.SetBool(P_ATTACKING, true);
 }


For the next part I think you need OnMouseUpAsButton


https://docs.unity3d.com/2018.3/Documentation/ScriptReference/MonoBehaviour.OnMouseUpAsButton.html



"OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed."


 public Button attackButton;

 void Start()
 {
     attackButton.OnClick.AddListener(StartAttack);
 }

 private void StartAttack()
 {
     anim.SetBool(P_ATTACKING, true);
 }

 private void OnMouseUpAsButton()
 {
     anim.SetBool(P_ATTACKING, false);
 }


This might not work with multiple buttons but you could probably hack it to make it work. Just check which button was pressed via a custom tag .

Comment
Add comment · Show 4 · 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 Cassos · Aug 11, 2020 at 11:58 PM 0
Share

Nice, just as a re$$anonymous$$der for all the newbies out there :)

FixedUpdate => for "Get Input" like $$anonymous$$ovement Update => for "GetDown or GetUp Input like attack, jump, or whatever

avatar image Pathojen · Aug 12, 2020 at 12:53 AM 0
Share

Crud... I just saw a bug... The ! after else if... I fixed that and it didn't work, though. I then copied and pasted your code, and the code seems to become true on GetButtonUp. I think this has something to do with it being UI, though.

avatar image Aviryx Pathojen · Aug 12, 2020 at 02:58 AM 0
Share

oh I re-read your question. I will change my answer.

avatar image Pathojen · Aug 12, 2020 at 12:35 PM 0
Share

Thanks! I found a small bug in your code. OnClick() should be onClick(). Thing is, now the animator's bool doesn't switch back. Will keep digging.

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

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

How to use a button to trigger an Animator Parameter 0 Answers

How do I play an animation once on button click? 1 Answer

UI button pressed animation not playing directly after entering 2 Answers

Animator has not been initialized?? 3 Answers

Button gets stuck in Pressed state 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