I want the player gets tired after running

I want the player gets tired after running, but i dont now how to do that :frowning: plz help!

var walkSpeed: float = 7; // regular speed
var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed

private var chMotor: CharacterMotor;
private var tr: Transform;
private var dist: float; // distance to ground

function Start(){
chMotor = GetComponent(CharacterMotor);
tr = transform;
var ch:CharacterController = GetComponent(CharacterController);
dist = ch.height/2; // calculate distance to ground
}

function Update(){

var vScale = 1.0;
var speed = walkSpeed;

if (chMotor.isGrounded && Input.GetKey("right shift") || Input.GetKey("left shift")){
    speed = runSpeed;
}
if (Input.GetKey("c")){ // press C to crouch
    vScale = 0.5;
    speed = crchSpeed; // slow down when crouching
}
chMotor.movement.maxForwardSpeed = speed; // set max speed
var ultScale = tr.localScale.y; // crouch/stand up smoothly 
tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position

}

make a boolean “running” and set true while running and a var stamina:int and set to how much he should be able to run before tired. Then:

function Update(){
  if(running){
    stamina = stamina-1 * Time.deltaTime;
  }
  if(stamina  <= 0){
    ///set runspeed lower
  }
}

i would probably use a float which decreases when running, you don’t want the user to be able to sprint a little stop sprint again straight away before ever slowing down.

so have a boolean which will switch from true and false when running, and if running is true then decrease the amount of stamina which is a float. and if running is false then slowly increase stamina/float back up.

then when stamina reaches 0 slow down until stamina reaches 25 or something, you could even get fancy and start to slow down the character slowly when the stamina float goes below 25