Stamina Bar

Hi, I’ve got it so that when i press shift i run forward (the usual) then my stamina timer starts only while i’m running and when i’m not running the stamina timer cools down. But i’m trying to get it so that when runTimer += Time.deltaTime - i cant run. Basically, while the stamina timer is cooling down, i cant run until its returned to 8. How do you do this? I’ve already tried:

        if(runTimer > 0.1 && runTimer < 7.9) {
            if (chMotor.grounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
                isRunning = false;

Here is my script:

var walkSpeed: float = 7; // regular speed
var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed
var stamina: boolean = false;
var runTimer: float = 8;
public var isRunning: boolean = false;
var myAnimation : Animator;
   
private var chMotor: CharacterMotor;
private var tr: Transform;
private var dist: float; // distance to ground


var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;
    
function Start(){
    chMotor = GetComponent(CharacterMotor);
    tr = transform;
    var ch:CharacterController = GetComponent(CharacterController);
    dist = ch.height/2; // calculate distance to ground
}
    
function Update(){
    var speed = walkSpeed;
    if(isRunning == true) {
        print("Speed is Running");
        speed = runSpeed;
        myAnimation.SetBool("Sprint",true);
        if(runTimer <= 0) {
            runTimer = 0;
            isRunning = false;
            myAnimation.SetBool("Sprint",false);
        }
    }
    if(isRunning == false) {
        print("Speed is Walking");
        myAnimation.SetBool("Sprint",false);
        speed = walkSpeed;
        if(runTimer >= 8) {
            runTimer = 8;
        }
    }
    if(stamina == true) {
        runTimer -= Time.deltaTime;
    }
    if(stamina == false) {
        runTimer += Time.deltaTime;
        if(runTimer >= 8) {
            runTimer = 8;
        }
    }
    //var vScale = 1.0;
    //var speed = walkSpeed;
    stamina = false;
    isRunning = false;
    
    if (chMotor.grounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
        isRunning = true;
        stamina = true;
    }

    if (Input.GetKey("c")){ // press C to crouch
    speed = crchSpeed; // slow down when crouching
    }
    chMotor.movement.maxForwardSpeed = speed; // set max speed
    }
   
function OnGUI () {
    GUI.Label(new Rect(300, 100, 200, 40), (runTimer.ToString()));
   
    
// draw the background:
GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
// draw the filled-in part:
GUI.BeginGroup (new Rect (0, 0, size.x * (runTimer * 0.13), size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
GUI.EndGroup ();
GUI.EndGroup ();
}

Thanks
-Fin

You’ll need to set a flag when your condition (run out of stamina) has occurred. Then, if that condition is true, don’t allow the player to run. And when the cooldown is complete, set it back to false.

What hammil said.

i.e. a bool called canRun would suffice.