# Moving platforms left and right between two walls

I'm currently working on a rocket game where the player needs to collect parts from each level and in the main menu they can upgrade their own rocket with the parts found. But now I'm trying to add platforms that the player can land on, these platforms need to move left and right randomly until they hit the side of the screen then they reverse in the opposite direction and repeat.

Problem is now I spawn the platforms in randomly on the X axis and I need them to move in a random direction(on the x axis) from the start. As the level moves up the screen I have a collider that generates more platforms further up.

Any Idea what I should try to achieve the desired result?

here is my current code to move the platforms into random directions.

public class MovingPlatform : MonoBehaviour
{
private float platformMin = -6.5f;
private float platformMax = 6.5f;

void Start()
{
}

{
GameObject[] platforms = GameObject.FindGameObjectsWithTag("Platform");

foreach (GameObject platform in platforms)
{
Vector3 pos = platform.transform.position;
pos.x = Random.Range(platformMin, platformMax);
platform.transform.position = pos;
}
}
}

• What have you tried with regards to movement? Currently, your code looks like it spawns the platforms in, then leaves them there. Do you have anything in your Update() routine? Sep 1 '16 at 14:07
• Not yet, I'm trying to figure out which route to take, Just spoke to a friend now and he mentioned that I should maybe just make the platform move between the min and max range and then when the min or max have been hit just send them in the opposite direction but honestly I'm so lost(I'm doing this at work on the side so my brain is almost burnt out) Sep 1 '16 at 14:13

We will assume you have the basics of a platform the player can physically land on. You need to move it back and

## Keeping Direction

Use a single integer to keep direction, restricting it to the values of 1 and -1. That way, you can always tell your platform to "move by the product of 'Right' and 'Direction'". If Direction = 1, your multiplying by 1, which gives no difference. If Direction = -1, your simply inverting your original value, giving you the opposite direction.

int direction = 1;
float speed = 5;

gameObject.Translate(Vector3.Right * direction * speed * Time.deltaTime);
// move object by ((1,0,0) * 1 * 5) units per second
//  = (5, 0, 0) units per second

direction = -1;

gameObject.Translate(Vector3.Right * direction * speed * Time.deltaTime);
// move object by ((1,0,0) * -1 * 5) units per second
//  = (-5, 0, 0) units per second


You can then randomise the initial value of direction on creation of each platform, just as you randomise the initial x position.

## When to Change Direction

You can trigger direction change in one of two ways: by checking position at each Update() or by checking for direction change at each OnCollisionEnter().

### via Update

During each Update(), you want to be moving your platforms. After each movement, simply check if they have reached the minimum or maximum distance, and toggle a direction change if they have. Note: This is a basic implementation, and it has its problems. For example, minDistance and maxDistance act as world-coordinates for the maximum border of the platform movement. Correcting movement past the borders is also a bit messier, but it gives you a rough idea of how to go.

float maxDistance = 10;
float minDistance = -10
int direction = 1;
float speed = 5;

void Update()
{
transform.Translate(Vector3.Right * direction * speed * time.deltaTime);

if(transform.position.x >= maxDistance)
{
transform.position = new Vector3(maxDistance,
transform.position.y, transform.position.z);
ChangeDirection();
}
else if(transform.position.x <= minDistance)
{
transform.position = new Vector3(minDistance,
transform.position.y, transform.position.z);
ChangeDirection();
}

}


### via Collision

Since the player can stand on the platforms, we assume they already have colliders on them. We can assume the walls also have colliders, to stop the player from moving through. One option you have is to detect the platform colliding with the wall, in order to change direction. This might complicate your game more than needed, due to at-least one party in the collision event requiring a rigidbody. This can lead to other undesirable effects, in turn, but the option is always there.

public void OnCollisionEnter(Collision other)
{
if(other.transform.tag == "Wall")
{
ChangeDirection();
}
}


## How to Change Direction

With everything else in place, changing direction should be quite easy. Best practice suggests you limit direct manipulation of direction to a single method, which you call whenever you wish to change direction. From here, you can check the current direction to determine the new direction, or use a simple math hack to ensure the switch.

### Checking Directly

ChangeDirection
{
if(direction == 1)
{
direction = -1;
}
else
{
direction = 1;
}
}


### Checking with Maths

ChangeDirection
{
direction *= -1;
}