- Home /
Block specific input for amount of time
I need to block a specific input from being able to be used for a few seconds after it is initially used.
#pragma strict
//create variables for the force inputs
var intYForce : int;
var intXForce : int;
var intZForce : int;
//create variable for angstyness
public var chuckingStrength : int;
//define d20fixed as a rigid body so we can do physics stuff
var d20fixed : Rigidbody;
function Start(){
//set initial value of chuckingstrength
chuckingStrength=10;
}
function Update () {
//throw the dice
if(Input.GetButtonUp("Jump")){
//set randoms for force inputs
intXForce=Random.Range(-10,10);
intYForce=Random.Range(1,10);
intZForce=Random.Range(-10,10);
//make sure they're not zero
while(intXForce==0){
intXForce=Random.Range(-10,10);
}
while(intZForce==0){
intZForce=Random.Range(-10,10);
}
//add force to the die
d20fixed.AddForce(10+intXForce*chuckingStrength,20+intYForce*chuckingStrength,10+intZForce*chuckingStrength);
d20fixed.useGravity=true;
//reset the die
Reload();
}
//make it so when you press the up button angst goes up
if(Input.GetButtonDown("up")){
chuckingStrength = chuckingStrength + 1;
if(chuckingStrength==21) {
chuckingStrength = 20;
}
}
//make it so when you press the down button angst goes down
if(Input.GetButtonDown("down")){
chuckingStrength = chuckingStrength - 1;
if(chuckingStrength==2) {
chuckingStrength = 3;
}
}
}
//function to wait 3 seconds and reload the level
function Reload(){
yield WaitForSeconds (3);
Application.LoadLevel (Application.loadedLevel);
}
Specifically, I need to make it so you can't press jump for a few seconds while the scene reloads.
I tried messing with the dead time of the input but it doesn't seem to be making a difference.
Obviously the yield waitforseconds isn't interrupting the space bar (jump) from being used while it's waiting.
I don't know where to go from here.
Answer by aldonaletto · Oct 13, 2013 at 11:04 PM
Use a boolean that says when Jump is blocked:
...
//define d20fixed as a rigid body so we can do physics stuff
var d20fixed : Rigidbody;
private var blocked = false; // create this variable
function Start(){
//set initial value of chuckingstrength
chuckingStrength=10;
}
function Update () {
//throw the dice - but only if not blocked!
if(!blocked && Input.GetButtonUp("Jump")){
...
}
//function to wait 3 seconds and reload the level
function Reload(){
blocked = true; // block the input
yield WaitForSeconds (3);
// unblock input - actually, this isn't necessary in this
// case because the level is going to be reloaded anyway!
blocked = false;
Application.LoadLevel (Application.loadedLevel);
}
thank you!! i didn't think to just turn it off and on like that
Answer by Cherno · Oct 13, 2013 at 10:57 PM
Set a boolean like "DisableJumpInput" to true, and when pressing the jump button, check if the boolean is set to false before calling the actual jumping function.
To enable jump input again, you could just check in Update() if the boolean is set to true, and if yes, call a function that waits a few seconds and then sets teh boolean back to false.
Answer by DylanW · Oct 13, 2013 at 11:09 PM
Use time.deltaTime to check the amount of time that has passed by. Once it reached or passed a specific value, set a flag that "unblocks" the input.
Please read the comments.
float waitTime = 10.0f;
bool doneWaiting = false;
void Update()
{
// Wait until waitTime is below or equal to zero.
if(waitTime > 0) {
waitTime -= Time.deltaTime;
} else {
// Done.
doneWaiting = true;
}
// Only proceed if doneWaiting is true.
if(doneWaiting && Input.GetButton("Jump")) {
Debug.Log("Jumped!");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613115858im_/https://answers.unity.com/themes/thub/images/avi.jpg)