Saving the starting position of a Lerp during Update

I have seen a number of examples for Vector3.Lerp where the original starting position is stored in the start function and then later used to compare the current position in relation to the end target position. My issue is that I need to store the starting position of the lerp in update not start, since the lerp will be happening on a gameobject being moved around the scene and therefore its starting position will need to be set each time manually. Here is my simple code which sits in update:

    if (doLerp) {
    				startPos = transform.position;
    				transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * speed);
    }

The “doLerp” is set to true whenever a lerp needs to take place and hence when a “startPos” needs to be stored. The issue with the above of course is that once the “startPos” is stored it will continue to be updated while “doLerp” is true, and since the lerp is changing the transform.position this will mean that the startPos will change as well instead of staying the same. How can I record the starting position of a transform before a lerp and not have it changed when the lerp begins?

I need this so that I can do calculations such as the distance between the starting and target positions, and then compare those to the distance between the current and target positions; and find out the percentage of the “trip” that has been completed.

You need to store initial position outside the Update function.

But in your case it’ll be the best to use coroutine:

IEnumerator MyLerp()
{
    if (!doLerp) yield break;
    doLerp = false;

    Vector3 initPos = transform.position;
    float elapsed = 0;
    while (elapsed < speed)
    {
        elapsed += Time.deltaTime;
        transform.position = Vector3.Lerp(initPosition, target.Position, elapsed / speed);
        yield return null;
    }
}

and in update (or better where you set startPos to true)

if (doLerp) {
    StartCoroutine(MyLerp());
}

Should work fine

Hey, I’m not much of a programmer, but on my third game, so I thought I’d comment.

Essential, I believe, your solution is already spelled out in your question/description.

You need to add another variable or perhaps two to the scripting. You need a “starting” variable, which is the very beginning one and doesn’t change.
Then you need a “current” variable (which you’ve got already, showing the current position when the dolerp is made true.
Lastly, you need the "target position " variable, which might or might not change, according to your game.

So, I believe that your choice lies essentially in where to record/set the initial/starting position variable. Do you want to set it as soon as the object is created/spawned? Then use void Awake () { to get that right at the beginning.
Otherwise, you may prefer to use a separate script to record this, if you must set the initial location later, perhaps with a collider of some sort, if this object is moving. (Set the collider around the object, so when it starts moving, it fires the script which record the colliding object’s locations.

Sorry for the generalizations. Just trying to help that creative spark continue onward!