How do I make a script in unity 2d in which a platform moves for x time to the right and for x time to the left
Try something like this - you can have a flag that signals whether you are moving left or right, and a timer that every 4 seconds swaps this flag. You can even modify the time and speed of the platform from the inspector. Just attach this script to your platform.
public class PlatformMovementScript : Monobehavior{
public float time = 4f;
public float speed = 1f;
private bool movingRight = true;
private float timer = 0f;
void Update(){
float leftOrRight = movingRight ? 1f : -1f;
transform.position += new Vector2(speed * Time.deltaTime * leftOrRight, 0f);
timer += Time.deltaTime;
if(timer > time){
timer = 0f;
movingRight = !movingRight;
}
}
}
**
Keep in mind that in order for a player to be able to stand on the platform, the player will need either very high friction if it is physics-controlled or you will need to make the player a child of the platform while the player is on it.