The other answers page with a similar question wasn’t answered for me as I have different (slightly) circumstances. With mine , here’s a problem.
I’m constantly updating my HEALTHUI (FillAmount) for UI with the actual health amount divided by 10.
What I would like to do is this:
HealthUI.fillAmount = Mathf.Lerp(Health/10, Health/10, Time.DeltaTime * Smooth);
For it to Lerp between old health value to new health value using the same variable?
How would you like an over-engineered and untested solution?
public class LerpVariable
{
float lerpTime = 0;
float targetValue;
float? currentValue;
public float Value
{
get
{
return (currentValue.HasValue)
? Mathf.Lerp(currentValue.Value, targetValue, lerpTime)
: 0;
}
}
public float TargetValue
{
get { return targetValue; }
set
{
if (currentValue.HasValue == false)
{
currentValue = value;
}
else
{
currentValue = Value;
}
lerpTime = 0;
targetValue = value;
}
}
public void UpdateValue()
{
lerpTime = Mathf.Clamp(lerpTime + Time.deltaTime, 0, 1);
}
}
Usage:
LerpVariable Health = new LerpVariable();
if (isHealing && Health.TargetValue < 100)
{
Health.TargetValue = Health.TargetValue + (0.1f * Time.deltaTime);
}
if (wasShot)
{
Health.TargetValue -= 40;
}
UpdateHealthBar(Health.Value);