Something's amiss, trying to implement stamina

I was messing around with the FPS Walker script, and I added a run feature to it. When running the jump strength is also greater. It works like a charm when I hold down my little run button. It simply multiplies the jump and run speeds when the key is held down.

I then decided to allow the character to have stamina, and began trying to think up how to do this. Seeing as I am a moron when it comes to scripting, I knew I wouldn’t get it right the first time.

I added a staminaPoints and a maxStaminaPoints variable at the top of the script. I added a Drain and Recharge variable as well.

staminaPoints = 100.0;
maxStaminaPoints = 100.0;
drain = 1.0;
recharge = 1.0;

I decided to first see if I could make stamina decrease when the “run” key was pressed.

I added,

if (Input.GetKey ("e")) {
      moveDirection *= speed * 2;
      if (speed > 8);
      staminaPoints -= drain;

and indeed, when I pressed play and held “e” the stamina decreased. I realize it wasn’t affecting movement and was just a counter at the moment, I just wanted to test out what I had so far.

I then added

	moveDirection *= speed * 2;
	if (speed > 8);
        staminaPoints -= drain;
	if (speed < 9);
	staminaPoints += recharge;
				}

in the hopes that the stamina counter would decrease while I held “run” and start counting up once I released it. It will not count up or down at this point unless I comment out the last 2 lines above.

I tried an if, and an else if, but that didn’t work either. Anyone have a suggestion? I thought about checking the players velocity over time or something, but then I realized I had no idea how to do that :sweat_smile:

There are semicolons after your IF conditions. A semicolon in IF statement without brackets denotes the end of that statement.

Well, it’s just a matter of adding an if statement. Basically, something like this:

if (Input.GetKey(KeyCode.Shift)) {
  if (Input.GetKey(KeyCode.E) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.F)) {
    staminaPoints -= drain;
  }
  else {
    staminaPoints += recharge;
  }
}
else {
  staminaPoints += recharge;
}

You’d want to replace my letters with your own movement keys, but it essentially says “If shift is held down and any movement key is held down, decrease stamina. Otherwise, increase it.”

Aha!

Thanks for quick and informative reply!

:smile: