How to LERP to saved coordinates?

I’m trying to create a cube prefab that I can place in my scene so that it will save its initial transform.position, transform to a random far away position and then LERP back to it’s initial position on update. I’ve tried instantiating an empty game object at transform.position and trying to have the cube LERP to it, but I can’t get it to automatically accept the cube in the destinationCube gameobject, and I’m not sure how to get the cube to LERP to the saved transform.position in update.

public class MovingCube : MonoBehaviour
{
    public E_MoveType moveType = E_MoveType.NONE;

    //Title cube jumps to here on starts and lerps to the destination on update
    public float minRangeX = 0.0f;
    public float maxRangeX = 0.0f;
    public float minRangeY = 0.0f;
    public float maxRangeY = 0.0f;
    public float minRangeZ = 0.0f;
    public float maxRangeZ = 0.0f;

    // Use this for initialization
    void Start()
    {
        Vector3 initialPosition = transform.position;

        //transform to random possition
        transform.position = new Vector3(transform.position.y, transform.position.z, Random.Range(minRangeX, maxRangeX));
        transform.position = new Vector3(transform.position.x, transform.position.z, Random.Range(minRangeY, maxRangeY));
        transform.position = new Vector3(transform.position.x, transform.position.y, Random.Range(minRangeZ, maxRangeZ));

        //then go to update and lerp to coordinates
    }

    // Update is called once per frame
    void Update()
    {
        if (moveType == E_MoveType.LERP)
        {
            transform.position = Vector3.Lerp
                (transform.position, initialPosition(WRONG according to Visual Studio), Time.deltaTime);
        }
        else if (moveType == E_MoveType.MOVE)
        {
            transform.position = Vector3.MoveTowards
                (transform.position, initialPosition(WRONG according to Visual Studio), Time.deltaTime);
        }
    }
}

public enum E_MoveType { NONE, LERP, MOVE };

You declared your “initialPosition” variable inside your Start method. That means the variable is a local variable of the Start method and does not exist outside that method. You want to declare it at class level so it becomes a member variable of the class and just assign the starting position inside Start.

public class MovingCube : MonoBehaviour
{
    // [ ... ]
    
    Vector3 initialPosition; // declare here
    
    // [ ... ]
    
    void Start()
    {
        initialPosition = transform.position; // assign here
        // [ ... ]

You are on the right path, but there are some problems with the code, and it can also be greatly simplified.

initialPosition is “wrong”, because the variable does not exist in that context (block); it was declared only in Start(), so it is local to that function.

The correct use of any lerp function is to pass it a slowly increasing variable in the range [0, 1]. So you need to store the time that has passed since lerping yourself, and divide that value by the duration you want the lerp to happen.

I’m not familiar with the use of Vector3.MoveTowards() (personally I think it’s a bad function), but you would need to pass as the third parameter a maxMovementSpeed variable multiplied by Time.deltaTime.

The simplifications: you can store related 3D values values like minRange* and maxRange in a Vector3. Random.Range() is an amazing function to generate a random value within a range.

So with this explained, the above script improved (I’ve also moved independent parts into functions):

public class MovingCube : MonoBehaviour {

    [SerializeField] private E_MoveType moveType = E_MoveType.NONE;

    [SerializeField] private Vector3 minRange = Vector3.zero;
    [SerializeField] private Vector3 maxRange = Vector3.one;

    [SerializeField] private float lerpDuration = 1;

    private Vector3 initialPosition;
    private Vector3 randomPosition;

    private float lerpTime = 0;

    private void Start () {
        initialPosition = transform.position;
        MoveToRandomPosition();
    }

    private void MoveToRandomPosition () {
        randomPosition = new Vector3(Random.Range(minRange.x, maxRange.x), Random.Range(minRange.y, maxRange.y), Random.Range(minRange.z, maxRange.z);
        transform.position = randomPosition;
    }

    private void Update () {
        switch (moveType) {
            case E_MoveType.LERP: DoLerp(); break;
            case E_MoveType.MOVE: DoMove(); break;
        }
    }

    private void DoLerp () {
        lerpTime += Time.deltaTime;
        transform.position = Vector3.Lerp(randomPosition, initialPosition, lerpTime / lerpDuration);
    }

    private void DoMove () {
        transform.position = Vector3.MoveTowards(randomPosition, initialPosition, lerpDuration);
    }

}

public enum E_MoveType { NONE, LERP, MOVE };

Thank you all!