- Home /
Please Help Me With My Stamina Bar!
Basically now matter How I Write The Code For Stamina It Will Not Work I Tried It 2 Different Ways And still Nothing. What is supposed to happen when I hold left shift and run stamina is suppose to decrease when it reaches 0 it's supposed to make the player walk and then stamina will increase while player is walking or stopped, stamina is also supposed to effect Hunger and Thirst and make them decrease faster then normal. Hunger and thirst work, Stamina is giving me problems. do you See Something Wrong With My Code, Am I Missing Something in it? If So What is it? Please Help I am New To C# And Unity.
Code:
public float Health;
public float HealthOverTime;
public float Hunger;
public float HungerOverTime;
public float Thirst;
public float ThirstOverTime;
public Slider HealthBar;
public Slider HungerBar;
public Slider ThirstBar;
public Slider StaminaBar;
public float MaxStamina;
private float StaminaDecreaseRate;
public float StaminaDecreaseMult;
private float StaminaIncreaseRate;
public float StaminaIncreaseMult;
public float MinAmmount = 0f;
private Rigidbody MyBody;
private PlayerMovement PlayerController;
void Start()
{
HealthBar.maxValue = Health;
HungerBar.maxValue = Hunger;
ThirstBar.maxValue = Thirst;
StaminaBar.maxValue = MaxStamina;
StaminaDecreaseRate = 1;
StaminaIncreaseRate = 1;
MyBody = GetComponent<Rigidbody>();
PlayerController = GetComponent<PlayerMovement>();
updateUI();
}
void Update()
{
CalculateValues();
}
private void CalculateValues()
{
Hunger -= HungerOverTime * Time.deltaTime;
Thirst -= ThirstOverTime * Time.deltaTime;
if (Hunger <= MinAmmount || Thirst <= MinAmmount)
{
Health -= HealthOverTime * Time.deltaTime;
MaxStamina -= StaminaDecreaseRate * Time.deltaTime;
}
if (MyBody.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
StaminaBar.value -= Time.deltaTime / StaminaDecreaseRate * StaminaDecreaseMult;
}
else
{
StaminaBar.value += Time.deltaTime / StaminaIncreaseRate * StaminaIncreaseMult;
}
if (StaminaBar.value >= MaxStamina)
{
StaminaBar.value = MaxStamina;
}
else if (StaminaBar.value <= 0)
{
StaminaBar.value = 0;
PlayerController.Runspeed = PlayerController.WalkSpeed;
}
else if (StaminaBar.value >= 0)
{
PlayerController.Runspeed = PlayerController.RunspeedNorm;
}
if (Health <= 0f)
{
print("You Died");
}
updateUI();
}
private void updateUI()
{
Health = Mathf.Clamp(Health, 0, 100);
Hunger = Mathf.Clamp(Hunger, 0, 100);
Thirst = Mathf.Clamp(Thirst, 0, 100);
MaxStamina = Mathf.Clamp(MaxStamina, 0, 100);
HealthBar.value = Health;
HungerBar.value = Hunger;
ThirstBar.value = Thirst;
StaminaBar.value = MaxStamina;
}
Well it would be helpful if you explain what is the expected behavior and how it does not work
Answer by Casiell · Feb 12, 2019 at 03:38 PM
I'm assuming you mean that your stamina regen and stamina decrese when holding shift do not work. (lines 76-89)
That's because those lines do absolutely nothing.
You are modifying your slider value, but at the end you still assign MaxStamina as your current stamina slider value (line 125)
Took out line 125 and the sta$$anonymous$$a bar Decreases and Regains But It Starts At 0 and hast to regain at the start I want it to b when I start the game start at 100 Sta$$anonymous$$a and then Decrease if you press shift to run and when I run out of sta$$anonymous$$a I want the player to walk. That Is Not Happening, He Can Continue to Run after Running Out Of sta$$anonymous$$a. How Can I fix This?
Edit: I figured It out. All I Had To Do Is Change A couple Things Around
else if (Sta$$anonymous$$aBar.value <= 0)
{
Sta$$anonymous$$aBar.value = 0;
PlayerController.$$anonymous$$axSpeed = PlayerController.WalkSpeed;
}
else if (Sta$$anonymous$$aBar.value >= 0)
{
PlayerController.Runspeed = PlayerController.RunspeedNorm;
}
Your answer
Follow this Question
Related Questions
Issue with implementing my own slider script. 1 Answer
Button only works once 1 Answer
How do I save and load the value of a text? 1 Answer
[C#] Unity asks for a } for a reason I don't know ( '}' expected ) 1 Answer
"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer