SkoolProject: Cooldown/Stamina when running! (GUI would be even more appreciated)

Hey Guys,
I’m working on a Skool Project with some guys and it’s a First-Person Game. We almost got everything done, but we do only need Cooldown/Stamina for running. So, when you run the stamina goes down and when it’s empty (the bar) that you temporarily can’t run and when it’s full again you can… and so on.
So, any help would be really-Really appreciated for our project! :slight_smile:

Here is our GameController.Js for so far,

var speed = 30.0; 
var jumpSpeed = 8.0; 
var gravity = 20.0; 
var jumpCount = 0; 
var maxJump = 1; 

private var moveDirection = Vector3.zero; 
private var grounded : boolean = false; 

private var mainCamera : GameObject = null;
private var controller : CharacterController = null;

function Start () 
{
    Screen.showCursor = false;
    mainCamera = GameObject.FindWithTag("MainCamera");
    controller = GetComponent(CharacterController);
}


                              //Reset jumpCount

   function OnCollisionEnter() 
    { 
       jumpCount = 0; 
    } 

   function Update() { 



       if (!grounded) { 
       moveDirection.x = Input.GetAxis("Horizontal")*speed; 
       moveDirection.z = Input.GetAxis("Vertical")*speed; 

       } 

    else { 

            moveDirection = Vector3(Input.GetAxis("Horizontal")*speed, 0.0,                 Input.GetAxis("Vertical")*speed); 

      //reset jump because we are grounded

            jumpCount = 0; 
      } 
    //jump
       if (jumpCount < maxJump) {
         if (Input.GetButtonDown ("Jump"))  { 
         moveDirection.y = jumpSpeed; 
        jumpCount++;
      } 
}
   moveDirection = transform.TransformDirection(moveDirection); 

   // Apply gravity 
   moveDirection.y -= gravity * Time.deltaTime; 

   // Move the controller 
   grounded = (GetComponent(CharacterController).Move(moveDirection * Time.deltaTime)        &  CollisionFlags.CollidedBelow) != 0; 


          //RUNNING

       if (Input.GetKey (KeyCode.F)) 
         speed = 50.0; 

    else //
         speed = 30.0;
} 


@script RequireComponent(CharacterController)

First, You’re going to need new variables. Of course, feel free to chosse your own names.

var regenStaminaSpeed : float = 1.0;
var staminaLossWhilstWalking : float = 1.0;
var staminaLossWhilstRunning : float = 5.0;
var regenStaminaSpeed : float = 1.0;
// It would be clever to use a property here and clamp it between 0 and staminaMax.
// I'm not comfortable with javascript though, if someone can help.
var stamina : float = 1.0; 
var staminaMax : float = 1.0;
var tiredSpeed: float = 10.0;
var walkSpeed: float = 30.0;
var runSpeed: float = 50.0;
private enum MovementType{ Walking, Running, Still };
var moveType: MovementType = MovementType.Still ; // The kind of movement your char is doing
                                                  // It's related to the key pressed

Then, the function of stamina modification

function StaminaEvolution()
{
    while( Application.IsPlaying )
    {
        var staminaDelta = moveType == MovementType.Still ? Time.deltaTime * regenStaminaSpeed
        : moveType == MovementType.Walking ? -Time.deltaTime * staminaLossWhilstWalking 
        : moveType == MovementType.Running? -Time.deltaTime * staminaLossWhilstRunning ;

        stamina = Mathf.Clamp( stamina + staminaDelta, 0, staminaMax );
        yield null;
    }
}

Finally, the calcul of the movement

// ...
if( stamina < 0.01 ) // Beware of float imprecision with ==
    speed = tiredSpeed;
else
    speed = Input.GetKey (KeyCode.F) ? runSpeed : walkSpeed;

You still need to modify moveType at the right moment, to call StaminaEvolution at start and to consider that the code is not tested and that I’m not used to javascript. Good luck !