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
1
Question by zeuo · May 12, 2012 at 04:41 PM · buttonpressed

how to chick weather the button is pressed or held down

sorry for my english, i have 1 button i need to chick if it is being pressed or held down how do i do this alt text

button.png (15.7 kB)
Comment
Add comment · Show 2
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 Kryptos · May 12, 2012 at 04:50 PM 0
Share

By button, do you mean keyboard, mouse or GUI?

And please read the manual, because your answer is there (for any of the case above).

avatar image zeuo · May 12, 2012 at 04:52 PM 0
Share

its gui, when i read the script reference it talked about the button and the GUI.RepeatButton but they are separate i want them to be the same button

1 Reply

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

Answer by aldonaletto · May 12, 2012 at 04:52 PM

You can use Input.GetButton("buttonName"): this function returns true while the button is pressed. There are equivalents for keys (Input.GetKey("a"), for instance) and for GUI ( GUI.RepeatButton)

EDITED: Well, doing what you want with GUI buttons is more complicated than I supposed: contrary to what I thought, RepeatButton doesn't return true while pressed - it actually returns true and false alternately each OnGUI! This complicates things, because there's no steady state to rely on.
But the show can go on: since Unity doesn't return a steady state for us, let's create our own! We can use a boolean flag, speedButton, which will be set when RepeatButton returns true and reset when the mouse button is released.
To simplify things a little, let's check the time only when RepeatButton is released: if it's greater than the limit, select high speed, otherwise select low speed:

var speed: float; var timeThreshold: float = 0.6; // 600mS to select high speed

private var speedButton = false; // current button state private var lastState = false; // last button state private var speedTime: float = 0; // time measurement

function OnGUI(){ // create a steady state result for RepeatButton: // set speedButton when RepeatButton is true... if (GUI.RepeatButton(Rect(10,10,200,100), "Speed")){ speedButton = true; } // and reset it when mouse button released: speedButton &= Input.GetMouseButton(0); // now speedButton can be verified: if (speedButton != lastState){ // if button changed state... lastState = speedButton; // register current state if (speedButton){ // if button down define the threshold time speedTime = Time.time + timeThreshold; } else { // if button up select speed according to time elapsed speed = (Time.time > speedTime)? 10 : 5; } } }

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 zeuo · May 12, 2012 at 05:05 PM 0
Share

but how do i make them the same button

avatar image zeuo · May 12, 2012 at 05:46 PM 0
Share

sorry it didnt work i replaced the speed =5 with print(low speed) and when i turn the game one the low speed is always there even with out pressing the button. my program$$anonymous$$g skills arnt good so i dont know how to fix it

avatar image aldonaletto · May 12, 2012 at 06:19 PM 0
Share

Ok, I'll check this - this logic is always confusing!

avatar image aldonaletto · May 12, 2012 at 11:36 PM 0
Share

Dear God! RepeatButton actually returns true and false alternately while pressed, what invalidates my previous algorithm!
To solve this, I edited my answer and added a boolean flag that is true while the RepeatButton is pressed and becomes false when it's released - take a look at my answer.

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

6 People are following this question.

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

Related Questions

Stop shooting when I press a gui button 1 Answer

increase size of object when button pressed 0 Answers

Is it possible to find which client/server pressed a GUI.Button? 0 Answers

Unity GUI Button Options 0 Answers

UI 4.6 left right button movement problem 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