x


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)
more ▼

asked May 12 '12 at 04:41 PM

zeuo gravatar image

zeuo
388 54 71 83

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).

May 12 '12 at 04:50 PM Kryptos

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

May 12 '12 at 04:52 PM zeuo
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
    }
  }
}
more ▼

answered May 12 '12 at 04:52 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

but how do i make them the same button

May 12 '12 at 05:05 PM zeuo

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 programming skills arnt good so i dont know how to fix it

May 12 '12 at 05:46 PM zeuo

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

May 12 '12 at 06:19 PM aldonaletto

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.

May 12 '12 at 11:36 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x820
x15
x3

asked: May 12 '12 at 04:41 PM

Seen: 960 times

Last Updated: May 12 '12 at 11:36 PM