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 /
  • Help Room /
avatar image
0
Question by TechNevio · Feb 24 at 04:00 PM · scripting problemscripting beginner

Stamina Consuption

Hey I am pretty new to scripting and I have seen similar questions here before, although I cant seem to find a solution to the problem I have. Basically, I have two scripts one of them is In my Character and one of them is in my Slider (Stamina Bar)

The slider is my stamina and it can regen and deplete. (Stamina Script) Though when I press shift suddenly all my stamina is gone (Character script)

The stamina script is fine, the problem is in the character. Why is this suddenly depleting ALL my stamina? I want it to deplete progressively when holding down shift. This is probably very simple but for an beginner this can be very time consuming. XD

  void Update()
 {
     if (Input.GetKey(KeyCode.LeftShift))
         StaminaBar.instance.UseStamina(15);
 }
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 asafsitner · Feb 24 at 04:11 PM

Remember that your game runs at X frames per second - usually at least 30. That means that each frame takes about 1 / 30 of a second (or ~0.033 seconds).
The Update function is called for each and every one of these frames, or at least 30 times a second, and each time it's called you're reducing the stamina by 15 points out of the total.


Even if you only press the button for 1/4 of a second, you're effectively calling the Update function about 30/4 times, or ~7-8 times. Multiply that by 15, and you get a stamina use of ~112 on average.


This is where Time.deltaTime comes in handy. This value is set to the time since the last frame was processed, or ~ 1/ 30 (or whatever frame rate your game is running at). What this means for you, is that you can "fix" the rate of stamina use from per-frame basis, to per-second basis.

If you multiply the rate of stamina use by Time.deltaTime, you'll get a gradual reduction of 15 points per second the button is held. From here you can tweak the stamina use value, add other multipliers, etc. to achieve the mechanics and feel you're going for.

Example code:

 class StaminaUser : MonoBehaviour
 {
     // this value will show in the Inspector for easy editing, but default to 15
     public float StaminaUseRate = 15;
 
     private void Update()
     {
         if (Input.GetKey(KeyCode.LeftShift))
             StaminaBar.instance.UseStamina(StaminaUseRate * Time.deltaTime); // multiply by Time.deltaTime to convert to per-second rate
     }
 }

Comment
Add comment · Show 3 · 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 TechNevio · Feb 24 at 04:26 PM 0
Share

Yeah I have tried something similar before but I am getting an Error at

"StaminaUseRate * Time.deltaTime" (Your example)

Only difference being that I called the Variable differently. Unity just says at that it cannot convert a float to an integer.

avatar image TechNevio · Feb 24 at 04:34 PM 1
Share

Thx for the help I figured it out now!!! Really frustrating if you are a beginner but Thank you a lot!!!

avatar image asafsitner TechNevio · Feb 24 at 07:40 PM 0
Share

You're welcome! I had difficulty with the concept of time and frames in the past, so glad I could help out :)

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

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

I need Function to be in other script 1 Answer

Physics2D.OverlapCircle working in Unity but not in build 0 Answers

Get color from ColorPicker and apply it randomly on GameObject 0 Answers

How can I make a float decrease when touched by a cube? 0 Answers

How do I assign values to variables in the Update function only ONCE? 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