- Home /
Can my lerp-function be optimized?
I have made this little tooltip-gameObject which is supposed to swap in and out new tooltips during the game.
I'm using a standard lerping functionality using a common "EaseOutBack"/"EaseInBack".
Now I could not do a standard Vector3.Lerp over fromPosition and toPosition, since the EaseOutBack routine goes beyond the bounds of from and to. So I had to do this:
IEnumerator SwapIn()
{
Vector3 fromPosition = new Vector3(10f, 0, 0);
Vector3 toPosition = Vector3.zero;
float lerpFactor = 0;
float duration = 1f;
float rate = 1.0f / duration;
float tweenX = 0;
float tweenY = 0;
float tweenZ = 0;
Vector3 localPosition = Vector3.zero;
while(lerpFactor <= 1.0f)
{
lerpFactor += Time.deltaTime * rate;
tweenX = EasingFunctions.EaseOutBack(fromPosition.x, toPosition.x, lerpFactor);
tweenY = EasingFunctions.EaseOutBack(fromPosition.y, toPosition.y, lerpFactor);
tweenZ = EasingFunctions.EaseOutBack(fromPosition.z, toPosition.z, lerpFactor);
localPosition.Set(tweenX, tweenY, tweenZ);
toolTipsContainer.transform.localPosition = localPosition;
yield return null;
}
toolTipsContainer.transform.localPosition = toPosition;
}
I really think this could be done prettier. In the future I would like to just use generic standard routines all over my game, so I could use an optimal representation of this snippet of logic.
Thank you in advance.
Answer by Owen-Reynolds · Apr 03, 2013 at 02:22 PM
That isn't doing any more work than a vector3 operation. In other words, the built-in Lerp also does math for x, then y then z. Regular CPUs don't have any special fast way to do things to Vectors all at once (graphic cards do.)
If you just want to save keystrokes, make a function out of those middle four lines: Vector3 easeOutBack(V3 F, V3 T, float pct) { V3 res; res.x=EasingFunction.Ease...(F.x, T.x, pct) ...
Your answer
![](https://koobas.hobune.stream/wayback/20220613095614im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Stopping Vector3.Lerp 3 Answers
Object transported to point instead of moving towards it? 1 Answer
Static Coroutine being called endlessly 2 Answers
Calculating collision behavior while using lerp 0 Answers
Lerping from one position to another in a specific amount of time, no matter the distance 0 Answers