Using Lerp() Properly In Unity
The Code
Make sure to actually use time in the correct way to interpolate between two values with Lerp() in Unity, i.e. don’t just throw Time.deltaTime in for the time parameter.
private IEnumerator RaisePositionRoutine()
{
Vector3 originalPos = transform.localPosition;
Vector3 startPos = originalPos - new Vector3(0f, _raiseDistance, 0f);
transform.localPosition = startPos;
float elapsed = 0f;
while (elapsed < _duration)
{
elapsed += Time.deltaTime;
float t = Mathf.Clamp01(elapsed / _fadeDuration);
transform.localPosition = Vector3.Lerp(startPos, originalPos, t);
yield return null;
}
// Ensure final state
transform.localPosition = originalPos;
}