- Home /
Using Coroutines to increase Float values C#
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.
It is not going up because you're checking if LeftShift is pressed in sta$$anonymous$$aGain function. Try removing the line: while(Input.Get$$anonymous$$ey(code)){ from the sta$$anonymous$$aGain function so that it looks like this.
public IEnumerator sta$$anonymous$$aGain(float delay, $$anonymous$$eyCode code){ sta$$anonymous$$a = sta$$anonymous$$a + 0.005f; yield return new WaitForSeconds(5); if(sta$$anonymous$$a > 1){ sta$$anonymous$$a = 1; }
your coroutine won't do anything unless LeftShift
is pressed when you call it - it'll just drop thru the while()
and exit.
have you defined "Sprint" to be LeftShift
too?
a few other things - take them with a grain of salt.
you're passing a parameter for a delay which isn't used - is that intentional? you're using canSprint
as a float
where it would be better to use a bool
.
private bool _canSprint;
(i name private variables starting with an underscore)
if it only has two states (1 or 0), then it's easier to understand and less likely that it'll become something other than 1 or 0. and to test for it ins$$anonymous$$d of
if (_canSprint == 1)
{...}
you can say
if (_canSprint)
{...}
and ins$$anonymous$$d of
sta$$anonymous$$a = sta$$anonymous$$a - 0.005f;
you can write
sta$$anonymous$$a -= 0.005f;
less to write, easier to modify and less likely to go wrong ;)
Answer by Voridian · Jul 17, 2014 at 10:02 AM
sorry to answer my own question but a combination of Saad_Khawaja's answer and adding the ! to:
if(!Input.GetButton("Sprint")){
if( stamina < 1){
StartCoroutine(staminaGain(1, KeyCode.LeftShift));
}
}
seemed to work, thanks to all for the help
you might want to revisit that following Bored$$anonymous$$ormon's advice - starting multiple coroutines when one will do the job will make your life easier..
No problem answering your own question. Glad we could be of assistance.
Answer by Kiwasi · Jul 17, 2014 at 09:52 AM
I would suggest throwing both the methods into a single coroutine. This makes more sense then starting and stopping both. It will simplify your Update as well. I believe the following code meets the intent of the original script, a little hard to tell what you were trying to do.
void Start (){
StartCoroutine(StaminaControl(KeyCode.LeftShift));
}
public IEnumerator StaminaControl(KeyCode code){
while (true){
while(Input.GetKey(code) && stamina > 0){
stamina = stamina - 0.005f;
if(stamina < 0){
stamina = 0;
}
movementSpeed = sprintSpeed;
yield return null;
}
while(!Input.GetKey(code) && stamina < 1){
stamina = stamina + 0.005f;
movementSpeed = originalMovementSpeed;
if(stamina > 1){
stamina = 1;
}
yield return null;
}
yield return null;
}
}
Answer by Tehnique · Jul 17, 2014 at 09:24 AM
You have your stamina lossing corutine set up ok, but you probably copy-pasted for gaining. In the gain function you have "while(Input.GetKey(code))" and it is wired to run on ButtonUp. It won't run, because Input.GetKey(code) will return false (since the user released the shift button, thus activating the ButtonUp).
Just replace the condition inside the gain while with something like "while(currentStamina < maxStamina)".
@Tehnique i tried replacing the "while(Input.Get$$anonymous$$ey(code))" like you said and also removing it like Saad_$$anonymous$$hawaja suggested but still nothing
Your answer
![](https://koobas.hobune.stream/wayback/20220613154313im_/https://answers.unity.com/themes/thub/images/avi.jpg)