Reducing a float as another increases
I'm working on a stamina system for a vertical slice of a game. The basic idea is that the player has a maximum stamina of 100, which can be reduced when the player is hungry. So if the player's hunger is at 75, the max stamina would be reduced by about 37.5 currently. I've also got it so it only starts decreasing the player's max stamina after they break the hunger threshold of 50. So I've got a functional iteration, but it seems pretty iffy, and it doesn't give me a lot of control, especially if I wanted to adjust the curve at which the maximum stamina of the player is reduced. Is there a better way of implementing a system like this? I'm keeping the stats in a separate script to keep things a bit cleaner. So here's the variables:
public class PlayerStats : MonoBehaviour
{
public float m_maxHealth = 100.0f;
public float m_minStamina = 10.0f;
public float m_maxStamina = 100.0f;
public float m_trueMaxStamina = 100.0f;
public float m_Weight = 70.0f;
public float m_Temperature = 37.0f;
public float m_Hunger = 50.0f;
}
And here's the code that calculates the stamina and the hunger:
private IEnumerator calculateHunger(float decreaseRate)
{
routineRunning = true;
float hunger = playerStats.m_Hunger;
yield return new WaitForSeconds(decreaseRate);
if (hunger > 100)
hunger = 100.0f;
else
{
if (isEating)
hunger = Mathf.Clamp(hunger -= baseHungerIncrease, 0, 100);
else
hunger = Mathf.Clamp(hunger += baseHungerIncrease, 0, 100);
}
playerStats.m_Hunger = hunger;
routineRunning = false;
}
private IEnumerator calculateStamina(float decreaseRate)
{
routineRunning = true;
float stamina = playerStats.m_maxStamina;
yield return new WaitForSeconds(decreaseRate);
stamina = Mathf.Clamp(stamina -= ((playerStats.m_Hunger - 50) * 0.5f), playerStats.m_minStamina, playerStats.m_trueMaxStamina);
if (stamina < 10)
stamina = 10;
playerStats.m_maxStamina = stamina;
routineRunning = false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612220206im_/https://answers.unity.com/themes/thub/images/avi.jpg)