hey all, i have an issue with my code where i am using coroutines to make my stamina float go up and down depending on whether the left shift key is pushed, the stamina value goes down but refuses to go back up again. sorry for the mess i have had to butcher other codes to get this working since i cant really understand coroutines:
float sprintSpeed = 5;
float canSprint = 1;
float originalMovementSpeed = 2;
public float health = 1.0f;
public float stamina = 1.0f;
void Update () {
//Sprint
if( Input.GetButton("Sprint")){
if( canSprint == 1){
if(stamina > 0.0f){
StartCoroutine(staminaLoss(1, KeyCode.LeftShift));
movementSpeed = sprintSpeed;
}
}
}
else{
movementSpeed = originalMovementSpeed;
}
if( Input.GetButtonUp("Sprint")){
if( stamina < 1){
Debug.Log("staminup");
StartCoroutine(staminaGain(1, KeyCode.LeftShift));
}
}
}
public IEnumerator staminaLoss(float delay, KeyCode code){
while(Input.GetKey(code)){
stamina = stamina - 0.005f;
yield return new WaitForSeconds(5);
if(stamina < 0){
stamina = 0;
}
}
}
public IEnumerator staminaGain(float delay, KeyCode code){
while(Input.GetKey(code)){
stamina = stamina + 0.005f;
yield return new WaitForSeconds(5);
if(stamina > 1){
stamina = 1;
}
}
}
Thanks for any help and i can post the full code if needed.