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;
}