Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Miaw666 · Nov 24, 2019 at 02:03 AM · movement scriptrunning

Stamina for running doesn't recharge like it should

I have a script for walking and running. When the player runs the speed changes and the "stamina" decreases and when the player isn't running the speed is back to normal and the "stamina" increases. When the Stamina is 0 and the player is holding down Shift, the Stamina shouldn't increase, the player should just not be allowed to move faster. But when the Stamina is less than 0 and the player IS holding down the run button, the stamina increases, and decreases very rapidly. I have a headbobbing script that is affected by the running, so when the Stamina is 0 the headbobbing speed gets faster and back to normal very rapidly, just like the stamina does. How do I fix it? Here's that relevant part of the script:

  if (Stamina > 0 && Input.GetKey(KeyCode.LeftShift) && cc.velocity.magnitude > 0f)
  {
      walkingSpeed = 16.0f;
      sideSpeed = 10.0f;
      Stamina -= Time.deltaTime;
      IsRunning = true;
  }
  else
  {
      walkingSpeed = 8.0f;
      sideSpeed = 5.0f;
      Stamina += Time.deltaTime * 0.75f;
      IsRunning = false;
  }
Comment
Add comment
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

1 Reply

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

Answer by HenriMR · Nov 24, 2019 at 02:14 AM

I think that what is happening is when your stamina < 0 your if-statement returns false and the else part is run. If you want the stamina not to recharge while shift is down, replace "else" with :

else if ( ! Input.GetKey(KeyCode.LeftShift))

Hope it works :)

Comment
Add comment · Show 5 · 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 Miaw666 · Nov 24, 2019 at 02:25 AM 0
Share

It's still not working, I don't get why, what you gave should've worked but apparently it didn't for some reason... When I run and the sta$$anonymous$$a is finished the bobbing is still faster than when normal (Thanks for the help <3) I've fixed it, and now it's like this:

     if (Sta$$anonymous$$a > 0 && Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) && cc.velocity.magnitude > 0f)
     {
         walkingSpeed = 16.0f;
         sideSpeed = 10.0f;
         Sta$$anonymous$$a -= Time.deltaTime;

         Headbobber.bobbingSpeed = 0.3f;
         Headbobber.bobbingAmount = 0.25f;

         IsRunning = true;
     }
     else if (!Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift))
     {
         walkingSpeed = 8.0f;
         sideSpeed = 5.0f;
         Sta$$anonymous$$a += Time.deltaTime * 0.75f;

         Headbobber.bobbingSpeed = 0.1f;
         Headbobber.bobbingAmount = 0.15f;

         IsRunning = false;
     }
avatar image HenriMR Miaw666 · Nov 24, 2019 at 02:31 AM 0
Share

Oh, I think that i should writen like this:

  else
      {
          walkingSpeed = 8.0f;
          sideSpeed = 5.0f;
 
 if (!Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)) { Sta$$anonymous$$a += Time.deltaTime * 0.75f; }
 
          Headbobber.bobbingSpeed = 0.1f;
          Headbobber.bobbingAmount = 0.15f;
 
          IsRunning = false;
      }
 

Just out of curiosity how did you solved it?

avatar image Miaw666 HenriMR · Nov 24, 2019 at 02:36 AM 0
Share

I'm sorry but what's over the else? And how did I solve what? Thanks for the help btw I'm sorry ;( I don't know what to replace with what, shouldn't there be something over the else? And something to make the speed faster?

Show more comments

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

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

How do I make a river in the terrain in unity? 3 Answers

Footsteps Script for Running and Walking 3 Answers

Problems with KeyCodes 2 Answers

Trigerrs Logic problem with collision damage 1 Answer

How do I Un-Clamp a Rigidbody2D's Magnitude 1 Answer


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