Lerp Problem

Hello guys,

I’m stuck with this, I have a platform witch has a Lerp function that allows it to move into a direction that I want, that works fine, the problem I have is that I can’t make it move back to the starting point again! (I want to create a moving platform that goes forward and back)

I’ve tried a lot of things but it’s not working (I have also tried the MoveTowards but is having the same result).

public class MovingPlatform : MonoBehaviour
{

    //Properties
    public float maxMoveWidth = 0.0f;
    public float maxMoveHeight = 0.0f;
    public float duration = 3.0f;

    //Saved status
    private Vector3 initialPosition;
    private Vector3 endPoint;
    private float startTime;
    private Vector3 lastPosition;
    private int direction = 1;
    private const int delay = 10;
    private int delayCount = 0;

    // Use this for initialization
    void Start()
    {
        initialPosition = this.transform.position;
        endPoint = new Vector3(initialPosition.x, initialPosition.y + maxMoveHeight, initialPosition.z + maxMoveWidth);
        startTime = Time.time;
    }

    // Update is called once per frame
    void Update()
    {
        if (delayCount >= delay)
        {
            lastPosition = this.transform.position;
            delayCount = 0;
        }
        else
        {
            delayCount++;
        }
        //transform.position = Vector3.Lerp(initialPosition, endPoint, Time.time);

            if (direction > 0)
            {
                transform.position = Vector3.Lerp(initialPosition, endPoint, Time.time);
            }
            else if (direction < 0)
            {
                transform.position = Vector3.Lerp(initialPosition, endPoint, Time.time);
            }
        if (this.lastPosition == this.transform.position)
        {
            direction *= -1;
            initialPosition = endPoint;
            if (direction < 0)
            {
                endPoint = new Vector3(this.transform.position.x, initialPosition.y - maxMoveHeight, initialPosition.z - maxMoveWidth);
            }
            else if (direction > 0)
            {
                endPoint = new Vector3(this.transform.position.x, initialPosition.y + maxMoveHeight, initialPosition.z + maxMoveWidth);
            }
        }

        //Debug.Log(direction); //(Time.time - startTime) / duration
        //Debug.Log("Pos: " + transform.position);
    }
}

With the code above, when the platform reaches the end it goes automatically to the starting point but it doesn’t do any animation. It flickers. O.o
What am I doing wrong?

Thanks!

Don’t use (Time.time), it returns the Time sincce the start of the game. You should use Time.deltaTime or more correctly create new variable and add Time.deltaTime and use this, once it reaches 1 reset it to 0

Many thanks!

I’ve solved it with your help.