Just... Not getting it right (key-hold timer)

This should be extremely simple for you programming geniuses, but I’m trying to get my player to hold onto the mouse button long enough to say “Yeah, the player means to shoot something”. So I tried doing this:

// This is the easiest and simplest weapon script in the entire fricken world!
// Only I adapted it with comments and things for my own character.

private var breathing : boolean = false;
private var holdingTime = 0.0;

function Update()
{

// Did the user release the firing button?
// Keynote here, I want two different animations, one for when I press the firing button,
// This will give me the breath in animation, and one for when I blow a bubble.

if (Input.GetMouseButton(0))
// A short little while loop to prevent the message from being brodcasted more than once.
while (breathing == false){
//BroadcastMessage("Breath");
holdingTime = Time.time;
breathing = true;
}


// Take a Deep Breath... Breath in...

if (Input.GetMouseButtonUp(0))
{
	if (Time.time > holdingTime + 0.10)
BroadcastMessage("Blow");
	else
	Debug.Log("Didn't Breathe long enough");
}
breathing = false;
// And then calmly breath out... Screw that, blow your lungs out!

}

But the problem is, it’s not doing my timing right, I’ve even held my mouse button down for 20 seconds and my script is still telling me I didn’t hold my mouse button down long enough… I’m pretty sure I’m missing something, but I don’t know what exactly.

BTW, the script works without my time restriction, I just want to add the hold-down timer in order to simulate breathing (and to give my breathe in animation a chance to play out)

if (Input.GetMouseButton(0)) {

  //increment holdBreathTicker by deltaTime
  holdingTime += Time.deltaTime;

}

if (Input.GetMouseButtonUp(0)) {
  if (holdingTime > 0.1) {
    BroadcastMessage("Blow");
  }
  else {
    Debug.Log("Didn't Breathe long enough");
  }

  //reset holdBreathTicker
  holdingTime = 0;

}

Ahh I see, I was using Time.time, not Time.deltaTime, got it!

Time.time could still work, but you’d need to change up the logic a bit.

if (Input.GetMouseButtonDown(0)) {

  //store the time that the mouse button was initially depressed.
  //Just a one-time operation.
  holdingTime = Time.time;
}

if (Input.GetMouseButtonUp(0)) {
  if (Time.time > holdingTime + 0.1) {
    //blow
  }
  else {
    //don't blow
  }
}

That’s originally what I had without all the rest of my script ^^;; Except I had the holdingTime in the while loop.

No it’s not; you were using GetMouseButton(0), not GetMouseButtonDown(0). The difference is that GetMouseButtonDown() returns true only in the frame following the button press, while GetMouseButton() returns true in each frame the button remains pressed.

(And BTW, the ‘while’ loop in your original code can only execute once, so an ‘if’ statement is all you need for that.)

Oh… I had GetMouseButtonDown before, but for some reason it just wasn’t working. As for the while, easy fix.