So I am building a staminia bar. Decreasing is working perfect, but increasing it is not working so well.
What I want is when I pick up a power piece to add stamina to the bar. But when it adds I want it to gradually go up. Not just jump up.
The code below simple makes it jump up. I have also tryied adding a time.deltatime, but when the trigger fires the object is deleted and the bar barely moves.
public void AdjustUp(int adj)
{
float tmpcuStamina = curStamina += adj;
if(tmpcuStamina > maxStamina){
curStamina = maxStamina;
}else{
curStamina = tmpcuStamina;
}
}
what am i doing wrong?
Hey there. So what’s going on here is that when you decrease your stamina, you’re likely doing it in small increments so it looks like its “smooth”. It’s not smooth because it’s meant to be smooth, it’s smooth because you’re just taking a little at a time.
Now, for the increase, you’re trying to add a bunch at once. Normally this would be fine, but you desire it to be smooth like the decrease. When you multiply by delta time, you’re literally only increasing stamina total by bonus*Time.deltaTime, a very small value. I’ll propose a solution for what I think you want:
Say you have a max stamina of 100 and a current stamina of 50. You pick up a buff that gives 30 stamina and instead of curStamina jumping to 80, you want it to maybe go up 30 units/second. What you could do here is have a few variables…
- CurStamina = 50 // The current stamina the player has
- displayStamina = 50; // The stamina displayed to the screen
- MaxStamina = 100; // The max stamina the player could get…
- stamFillSpeed = 30; // 30 units per second the stamina bar will fill or deplete
displayStamina is what you would be reflecting in the bar, and the number beside the bar could be curStamina. So the player knows he has 80 but it looks like the bar is “filling up” to 80. Here is how we do that…
void Update()
{
// This is saying that every frame, if what we're displaying isn't the true current value, adjust the bar with speed stamFillSpeed but don't exceed maxStamina or go below 0
if(displayStamina < curStamina)
{
displayStamina = Mathf.min(displayStamina + stamFillSpeed*Time.deltaTime, maxStamina);
}
else if(displayStamina > curStamina)
{
displayStamina = Mathf.max(displayStamina - stamFillSpeed*Time.deltaTime, 0.0f);
}
}
This could definitely be put in sort of an “adjust stamina” function that update calls every frame. So what you have now is a stamina bar that shows displayStamina in its graphic (So 50 at first, which matches curStamina). Then, you pick up the stamina bonus of 30 and curStamina jumps to 50 + 30 = 80. Now displayStamina is still 50, but when you enter the update the if sees this and starts incrementing displayStamina by speed (you can make it 30/second, 100/second, whatever). If speed is 30/second, the bar will increase by 30 points of stamina per second, which reaches 80 after 1 second and finally stops. This would also work for using stamina. Always adjust curStamina, and this logic will change the stamina you display to the player (displayStamina) in a smooth fashion. Hope this helps.
Still a little confused, but started to give it a try.
Not sure but in your update code I get the error UnityEngine.Mathf does not contain a definition for min
also says same thing for the max
Ah, small typo - I didn’t actually compile that code. It’s Mathf.Max and Mathf.Min, capitalized. What i’m doing with that code is smoothly moving the stamina bar (which should be displayStamina) to what you should have (curStamina). So you’ll always just be updating curStamina. If you pick up a stamina boost, curStamina will get increased by some amount. Then this code tells displayStamina to SLOWLY go to where curStamina is. This is what I think you wanted, the bar increasing over time instead of all at once. Remember to use displayStamina variable to represent the bar, not curStamina.
So here is some example code, hopefully no typos:
public void AdjustUp(int adj)
{
float tmpcuStamina = curStamina += adj;
if(tmpcuStamina > maxStamina){
curStamina = maxStamina;
}else{
curStamina = tmpcuStamina;
}
}
void adjustStamina()
{
// This is saying that every frame, if what we're displaying isn't the true current value, adjust the bar with speed stamFillSpeed but don't exceed maxStamina or go below 0
if(displayStamina < curStamina)
{
displayStamina = Mathf.Min(displayStamina + stamFillSpeed*Time.deltaTime, maxStamina);
}
else if(displayStamina > curStamina)
{
displayStamina = Mathf.Max(displayStamina - stamFillSpeed*Time.deltaTime, 0.0f);
}
}
void Update()
{
if(We hit a stamina boost)
{
AdjustUp(amount);
}
adjustStamina(); // This will slowly bring your stamina bar (displayStamina) to the curStamina value, instead of all at once.
}
Just figured that I out.
I really want to say thanks. Your information really helped me out!
Thanks much.
Great, I hope you get it working!