Hi, I am trying to make a toggle for a boolean. When I press the stance button the crouching boolean enables to true but i doesn’t change back when i press again.
var FirstPersonCamera: Component;
var crouching : boolean = false;
var stanceCooldown : float = 1.0;
var stanceCounter : float = 0.0;
function Update () {
stanceCounter += Time.deltaTime;
if (Input.GetButton("Stance")){
crouching = true;
stanceCounter = 0;
}
if (crouching && stanceCounter >= stanceCooldown){
if (Input.GetButton("Stance")){
crouching = false;
}
}
}
You’re doing some funky things in your script… like resetting stanceCounter if the button for stance it hit, no good. Try this.
var FirstPersonCamera: Component;
var crouching : boolean = false;
var stanceCooldown : float = 1.0;
var stanceCounter : float = 0.0;
function Update () {
if (crouching) {
stanceCounter += Time.deltaTime; // Update counter for stance.
// check to see if counter is greater than or equal to the cooldown max time... or since we know they are crouching... if they are and you hit the button, crouching is now false(toggle).
if (stanceCounter >= stanceCooldown || Input.GetButton("Stance")){
crouching = false; // set crouching to false
}
} else if (crouching == false && Input.GetButton("Stance")) {
crouching = true; // set crouching to true
stanceCounter = 0; // Reset stance counter
}
}