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);
}
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
}
}
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.
Thx for the help I figured it out now!!! Really frustrating if you are a beginner but Thank you a lot!!!
You're welcome! I had difficulty with the concept of time and frames in the past, so glad I could help out :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613053431im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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