Fake path follower with tween chain

This technique moves a game object along several segments on a path using a tween chain. (It supersedes path follower using a tween timeline in Phaser v3.55.)

const waypoints = [0, 0.5, 0.2, 1, 0.75, 0];

this.tweens.chain({
    loop: -1,
    tweens: waypoints.slice(0, -1).map((from, i) => ({
        targets: { value: from },
        value: { from, to: waypoints[i + 1] },
        // curveLength: the length of the path
        duration: 1 * curveLength * Math.abs(from - waypoints[i + 1]),
        completeDelay: 500,
        onUpdate: (tween, target, key, current) => {
            // curve: a path
            sprite.copyPosition(curve.getPoint(current));
        }
    })),
});
2 Likes