- Home /
The question is answered, right answer was accepted
1 Variable Affected By Multiple Variables
I would like to have a large float affected by multiple other floats. Right now I have this:
energy -= thirst + temperature + hunger + tiredness;
These variables get bigger every second which means the rate at which Energy decreases is vastly increased over time. I do not want this. Is there a way for energy to be decreased at a rate depending on these variables or would I have to reverse these variable's affects (such a decrease thirst instead of increase it and add the variables together to equal energy)
Answer by meat5000 · Jun 23, 2015 at 02:01 PM
Add some scale factors.
energy -= (a * thirst) + (b * temperature) + (c * hunger) + (d * tiredness);
a b c and d can be set to some decimal value (<1) to reduce the impact of the variables on the energy. You can use these as modifiable stats as an in game skill-tree or some nonsense so they player could acquire some resistance to temperature, for example.
So, above, if a = 0, thirst has no impact on energy. If a = 1, thirst has full impact (Or more if you make a > 1!!).
You go to a hot place. You can simulate this easily by increasing a and b to be > 1.
Answer by Ibzy · Jun 23, 2015 at 02:00 PM
My approach would be to first figure out what rate I would want energy to decrease at in the worst scenario. So with zero thirst, hunger, temp, and tiredness, the energy will not decrease as player is in top form. Assuming the 4 variables are 0-100:
int energyDecreaseRate = 1; //eg. 1 energy per frame
float energyDecreasePerc;
void Update(){
energyDecreasePerc = ((thirst+temp+hunger+tireness)/4)/100;
energy -= (1 * energyDecreasePerc) * Time.deltaTime;
}
What part are you struggling with?
This approach is giving the 4 variables equal effect on energy, and then reduces the energy by a percentage of your potential worst energy decrease(here "1").
e.g. Thirst:50, Temp:30, Hunger:21, Tiredness:73 50+30+21+73 = 174 / 4 = 43.5 /100 = 0.435 (43.5%) Energy is decreased by 0.435 per frame. At best state (just drank, eaten, slept and at average temperate) Energy will not decrease at all.
I have just noticed though, "tireness" should be "tiredness" and line 6 should read:
energy -= energyDecreaseRate * energyDecreasePerc) * Time.deltaTime;
Ah, I've found it out. $$anonymous$$ost of my variables are 0, so energy is just decreasing at a really slow rate. Thanks!
No, energy isn't an int, it's just that it's reducing really slowly, which is actually what I want.