# Move a platform left and right continuosly

Just a simple 2d scenario. I want the platform to move left and right continuously. The following code gets the platform to move right but when it comes to switching to left from there, it gets stuck. I understand that it is cos the if statements get into a lock at that portion but I am unable to figure out how I could solve this. Please advice. Thank you. The platform starting x position is at 0.

``````void Update () {

if (transform.position.x < 2.5)
{
movement = Vector3.right * 0.01f;
}

else if (transform.position.x > 2.5)
{
movement = Vector3.left * 0.01f;
}
transform.Translate(movement);
}
``````

If you want to move back and forth within a range you need to record your direction so it is consistent from frame to frame.

``````public float rightLimit = 2.5f;
public float leftLimit = 1.0f;
public float speed = 2.0f;
private int direction = 1;

void Update () {
if (transform.position.x > rightLimit) {
direction = -1;
}
else if (transform.position.x < leftLimit) {
direction = 1;
}
movement = Vector3.right * direction * speed * Time.deltaTime;
transform.Translate(movement);
}
``````