Tutorial: a moving platform

A platform that glides between two points, waits at each end and carries whatever stands on it.

1. Set up

Set up

Make a flat Cube with a collider and place it where it should start. Nothing else is needed.

2. The script

The script

Scripts/MovingPlatform.cs

public class MovingPlatform : Behaviour
{
    public Vector3 travel = new Vector3(6f, 0f, 0f);
    public float secondsEachWay = 3f;
    public float waitAtEnds = 1f;

    Vector3 home;
    Vector3 last;

    public override void Start()
    {
        home = transform.position;
        last = home;
        Go(true);
    }

    void Go(bool out_)
    {
        // one tween each way; when it ends, the next starts after the wait
        Tween.Move(gameObject, out_ ? home + travel : home, secondsEachWay, "inout").After(waitAtEnds).Then(() => Go(!out_));
    }

    public override void LateUpdate()
    {
        last = transform.position;
    }

    public override void OnCollisionStay(GameObject other)
    {
        // whatever rests on the platform moves by what the platform moved this frame
        Vector3 moved = transform.position - last;
        if (other.transform.position.y > transform.position.y) other.transform.Translate(moved);
    }
}

Save it as Scripts/MovingPlatform.cs and add it to the platform.

3. How it works

How it works

4. Try it

Try it

Press PLAY and jump on. Set travel to (0, 5, 0) for a lift, and change waitAtEnds for longer stops.

Next: checkpoints.