I want my player’s internal temperature to rise from 0 to 80 during a 90 degree rotation of the sun (morning to midday). It should increase with steady steps. If the player chooses to cool down, the player’s internal temperature should decrease by X number of degrees and then start rising again up to 80 (but only if the sun’s rotation is less than midday).
In the evening, the player’s internal temperature should decrease instead. The minimum internal temperature should be 0 at the end of the day.
I have tested with Mathf.Lerp but have problems when I want to reduce the temperature because my Mathf.Lerp code relies on the sun’s rotation, which causes Lerp to increase temperature speed up to 80 if I change the interpolation value to a smaller one.
I’d probably approach this fairly similar to your approach. Start with the basic Lerp you’re using to get the player’s base temperature. Then, assuming the player chooses to cool off, you can set a second property (let’s call it coolOffAmount) to 10, for example, to cool the player initially by 10 degrees (or however much you want). The coolOffAmount would reduce over time down to zero, and the total temperature would be your `Lerp - coolOffTemperature’. How quickly the coolOffTemperature approaches 0 could be easily dependent on the Lerp value (so, after cooling off in 90 degree temperature, they heat up quickly, but if they cool off when it’s 30 degrees out, they stay cooler longer.)
The problem is that if the player cool down really close to 90 degrees, it will just go right up to max temperature. That would be very bad I want it to go up in a steady way. It is kind of like in a spaceship and you deploy cooling to the engine. My character is a robot so it needs to keep internal temperature at a good temperature or it will start burning.
Maybe I understood you wrong? Do you have any example code for what you mean?
I think the approach I had in mind handles that. Here’s a mockup of the logic:
public float CoolOffDecayRate = 0.1f; // They warm back up at 0.1 degrees per second.
private float _coolingAmount = 0;
void Update() {
// Compute your Lerp-based temperature here, which is the base temp that
// ranged from 0 to 80 throughout the day.
var playerBaseTemp = Mathf.Lerp(0.0f, 80.0f, a);
// If the player has recently cooled off, reduce the impact of their cooling effect
// gradually down to 0. This provides then with some initial cooling, which slowly wears off.
var warmUpRate = Time.deltaTime * CoolOffDecayRate;// Optionally include the playerBaseTemp in this
// value to have the current daytime temp affect warmup rate.
// The cooling amount is reduced to a minimum of 0 over time.
_coolingAmount = Mathf.Max(0, _coolingAmount - Time.deltaTime * CoolOffDecayRate);
// The player's overall temp is the current daytime temp, minus the cooling
// amount.
Global._playerInnerTemperature = playerBaseTemp - _coolingAmount;
}
public void CoolOff() {
// Every time the player uses the "cooling" skill, it lowers their temp by
// 10 degrees. Change this to however much they should be cooled.
_coolingAmount = 10;
}
So, again, the general idea is that your Lerp-based temperature goes up and down over the course of the day, and that represents the base temperature of the player. The player can activate the “CoolOff” ability to temporarily lower their temperature. _coolingAmount represents the amount by which the player’s overall temperature is reduced. The _coolingAmount itself decays over time (as the player “warms back up”), until it reaches 0 and the player’s temperature is just the base daytime temperature again.
You can control how fast or slow you want the _coolingAmount to change. And you can optionally multiply the rate of change by the base temperature, so that the player loses _coolingAmount faster when it’s hotter out.
This approach would result in the player’s temp roughly following the curve you provided with your Lerp method, but reduced by an ever decreasing amount. For example, here’s a chart where blue is the normal base temperature throughout the day, and red is the player’s actual temp. The part I circled if where the player uses CoolOff, reducing their temperature compared to the base temp. Now notice that the red line gets closer and closer to the blue line, until they coincide, as _coolingAmount is reduced:
Yes I was thinking about that too. But I would still have to have some sort of increment over time. I have today made it so it adds 1 degree every x seconds and that works. I can easy change the speed of seconds to match my gametime. I have some options now so I can keep working at least