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 Ebil · May 23, 2013 at 08:08 PM · movementcontrollerbuttonscontrols

Best use of GetButton

Hello,

What is the best use to get all button states, my code now is:

 if(!Input.GetButtonUp("Left"))
         {
             if(Input.GetButton("Left"))
             {
                 log("Button Hold");
                 if(Input.GetButtonDown("Left"))
                 {
                     log("Button Down");
                 }
             }
         } else log("Button Up");

Usage is: User Pressed Button Down => an "object" is selected, if button goes up, it is still selected. While holding mouse down, object can be moved.

Other solution what I thought of to seperate it in three seperate if statements.

So what is best use-case ? Thanks in advance.

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

4 Replies

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

Answer by Ebil · May 24, 2013 at 07:54 PM

Well looks like I found a solution, nothing else had worked for me. Here my code, of course if someone uses it the functions has to be replaced. What it does : if object clicked, marked red as long as clicked. If button is not clicked anymore, object is yellow "still selected", if anything else is clicked than the desired object, selection is revoked. Correct layers has to be set to objects that are clickable.

 if(Input.GetButton("Left"))
          {
             Physics.Raycast( Camera.main.ScreenPointToRay( Input.mousePosition ), out hitInfo,100,(1<<8) + (1<<10));
             
             if(hitInfo.collider != null)
                 {
                     log(hitInfo.collider.gameObject.name);
                     currentObject = hitInfo.collider.gameObject;
                     currentObjectScene = currentObject.GetComponent<SceneObject>();
                     currentObjectScene.setIsActive(true);
                     log("Set Object Active");
                     
                 }
             else 
                 if(currentObjectScene != null)
                 {
                     if(currentObjectScene.getIsSelected()) 
                         currentObjectScene.reset(false,false);
                 }    
         }
         
         if(Input.GetButtonUp("Left"))
             {
                 if((currentObjectScene != null) && currentObjectScene.getIsActive())
                 {
                     currentObjectScene.setIsActive(false);
                     currentObjectScene.setIsSelected(true);
                 log("Set Object selected but not active anymore");
                 }
             }
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
1

Answer by Tomer-Barkan · May 23, 2013 at 09:07 PM

GetButton() will handle all the cases i think... so whenever the button is pressed, make sure the object is selected, and move it.

 if (Input.GetButton("Left")) {
     if (!selected)
         selectObject();
 
     moveObject();
 }
Comment
Add comment · Show 1 · 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 Ebil · May 23, 2013 at 10:09 PM 0
Share

I should have been more precise, there has are actions that are also used by all methods, i think with just getbutton it isnt the best use-case to solve this. Thanks for your answere.

avatar image
1

Answer by Graham-Dunnett · May 23, 2013 at 09:09 PM

 if (holding)
     log("Button hold");

 if (Input.GetButtonDown("Left")){
     holding = true;
     log("Button down");
 }
 if (Input.GetButtonUp("Left")){
     holding = false;
     log("Button up");
 }

Comment
Add comment · Show 1 · 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 Ebil · May 23, 2013 at 10:09 PM 0
Share

Looks good to me I will try this out Thanks.

avatar image
0

Answer by darthbator · May 23, 2013 at 10:11 PM

GetButton will register as true as long as the button is done. IMO the only uses for the other cases is if you want an action only ONCE on a button press (OnButtonDown) or if you want to hook into the use releasing the button. Outside of those cases I use normal GetButton for everything.

Comment
Add comment · Show 1 · 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 Ebil · May 23, 2013 at 10:20 PM 0
Share

and thats exactly what i have to do, as i write action happens if button is pressed down, i just forgot there are also actions if its moved up (object still selected but not active anymore), there are also actions in GetButton that the others are sharing.

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I set up my players controller script (How do I change the controls used to move and look) 2 Answers

CharacterController unexpectedly moving up 0 Answers

Script behaves differently when using a pad 1 Answer

What's the best alternative to Character Controller? 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