Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Voridian · Jul 17, 2014 at 09:15 AM · c#coroutinefloatstamina

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Saad_Khawaja · Jul 17, 2014 at 09:26 AM 0
Share

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; }

avatar image gjf · Jul 17, 2014 at 09:29 AM 0
Share

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 ;)

avatar image gjf · Jul 17, 2014 at 09:30 AM 0
Share

@saad - please use code tags

avatar image Voridian · Jul 17, 2014 at 09:49 AM 0
Share

@gif, thanks for the housecleaning tips, i will definitely use your advice on that

@Saad_$$anonymous$$hawaja ok i removed the line but it still dosnt work

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Jul 17, 2014 at 10:07 AM 0
Share

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..

avatar image Kiwasi · Jul 17, 2014 at 10:18 AM 0
Share

No problem answering your own question. Glad we could be of assistance.

avatar image
1

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)".

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Voridian · Jul 17, 2014 at 09:51 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Float not working all the time? 1 Answer

Multiple Cars not working 1 Answer

how to make a coroutine finish first before other coroutine start 1 Answer

How can i change a variable on the object hit by a raycast C# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges