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;
}
Update()
routine? \$\endgroup\$