Movement boost with cooldown

I’m trying to make temporary movement boost while holding left shift. It works like stamina in Elder Scrolls games, you wait to get more stamina so you can boost yourself.
It seemed to work, but when the stamina fully drains out it doesn’t recharge.
Here’s my script

public float BoostTime = 2;
public float stamina;
public float speed;

void Start(){
stamina = BoostTime;
}

void Update()
{

if(Input.GetKeyDown(KeyCode.LeftShift) && !Boosted)
{
Boosted = true;
}

if(Input.GetKeyUp(KeyCode.LeftShift) || stamina < 0)
{

stamina += Time.deltaTime;
Boosted = false;
if(stamina >= BoostTime)
{
stamina = BoostTime;

}
}
if(Boosted){
speed = 50f;
stamina -= Time.deltaTime;
}
if(!Boosted)
{
speed = 20f;
}```

Hey and welcome!
GetKeyUp is only true for one frame; the frame in which the key was released.
You intend to use something like !GetKey instead.

Edit: I would probably handle it a bit differently. When you detect a KeyDown, set boosted to true. When you detect a KeyUp set boosted to false. Now handle everything based on boosted. If it’s true, run faster and deplete stamina, if it’s false run normally and recharge stamina.

2 Likes

thanks