# Why is my moving platform getting stuck?

I’m trying to do a moving platform between an array of transforms and its positions. Here’s the code (sorry if it’s really obvious, but it’s my first time trying a engine).

``````    public float speed = 1;
public Transform[] positions;
private string direction = "Towards";
private int counter = 0;

void Start()
{
if (positions.Length > 0 || positions == null)
transform.position = positions[0].position;
}

void Update()
{
if (positions.Length < 2 || positions == null)
return;
if (counter < positions.Length - 1 && direction.Equals("Towards"))
counter++;
else if (counter > 0 && direction.Equals("Backwards"))
counter--;
if (counter == positions.Length - 1)
direction = "Backwards";
else if (counter == 0)
direction = "Towards";
transform.position = Vector3.MoveTowards(transform.position, positions[counter].position, speed * Time.deltaTime);

}
``````

@ricarditomontserrat, I don’t know, but it is too complicated. Simplify it!

The following assumes position 0 moves to the left, and position 1 moves to the right, and transform.position is already on the right at the start of this code.

So well start out moving left first. If you want to start on the left and move to the right first, then set counter = 1 at the start:

``````int counter = 0;

void update() {
if (positions.Length < 2 || positions == null) {
if( ( transform.position - positions[counter].position ).magnitude < 0.001 ){  // we reached the destination!
counter = (counter + 1) % 2 // alternate between 0 and 1
}
transform.position = Vector3.MoveTowards(transform.position, positions[counter].position, speed * Time.deltaTime);
}
}
``````

a note about my personal preference: I find code easier to follow if you let blocks of code return naturally without placing returns in the middle.