- Home /
Lerp on method
i need to call a method that calculates a value and then lerp another value using the calculated value,the problem is that i only need it to calculate once and continue the lerp afterwards is there some way to do this using only one method?
here is the code:
void Update()
{
LerpFogDensity(20, 5, );
}
public void LerpFogDensity(float Value, float ValueinSeconds)
{
var _temp = (Mathf.Abs(FogFloat - Value))/ ValueinSeconds;
FogFloat = Mathf.MoveTowards(FogFloat, Value, _temp* Time.deltaTime);
}
Comment
Answer by rage_co · Jul 26, 2021 at 03:30 AM
An Answer to this can be be using Coroutines.....
public IEnumerator LerpFogDensity(float Value, float ValueinSeconds, bool first, float continue_temp)
{
float _temp = 0f;
if(first)
{
_temp = (Mathf.Abs(FogFloat - Value))/ ValueinSeconds;
}
else
{
_temp = continue_temp;
}
FogFloat = Mathf.MoveTowards(FogFloat, Value, _temp* Time.deltaTime);
if(!Mathf.Approximately(FogFloat, _temp))
{
yield return 0;
LerpFogDensity(Value, ValueInSeconds, false, _temp);
}
else
{
FogFloat = _temp;
}
}
after this you can call it from anywhere once like this...(the fourth parameter can be any random value, since it'll be calculated anyway)
StartCoroutine(LerpFogValue(20f, 5f, true, 0f));
Hope this helps!!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Moving and Attacking 0 Answers
Learning Unity Methods 1 Answer