# Riding moving Platforms?

**URL:** <https://phaser.discourse.group/t/riding-moving-platforms/7330>\
**Category:** Phaser 3\
**Created:** [September 2, 2020, 8:36am UTC](https://phaser.discourse.group/t/riding-moving-platforms/7330 "2020-09-02T08:36:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kittykatattack](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/kittykatattack/32/3888_2.png) [@kittykatattack](https://phaser.discourse.group/u/kittykatattack)\
**Post date:** [September 2, 2020, 8:36am UTC](https://phaser.discourse.group/t/riding-moving-platforms/7330/1 "2020-09-02T08:36:28Z")

</div>

Hi Everyone,

I’m working on the game with tweened moving plaftorms, but my player character won’t “stick” to the platform on the X direction.

![ezgif-4-2dcec4486f99](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/0/053c5a624d0077234713c7db369abf23c27471a9.gif)

Both the player and platform bodies have an X friction of 1.

For reference, this is the platform physics group:

```javascript
const movingPlatforms = scene.physics.add.group({
    allowGravity: false,
    immovable: true,
  });

```

The player is has immovable set to `false`. I’m wondering: is there a simple setting I’m missing, or will I need to go the route of grabbing the platform’s velocity and adding it to the player object’s?

Thanks!!

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [September 2, 2020, 3:27pm UTC](https://phaser.discourse.group/t/riding-moving-platforms/7330/2 "2020-09-02T15:27:53Z")

</div>

[Friction won’t work with tweened sprites](https://codepen.io/samme/pen/WaqLdN "Path follower w/ Arcade Physics"). 🙁

You can maybe fake it somehow in a collision callback.

---

<div class="post-metadata">

**Author:** ![kittykatattack](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/kittykatattack/32/3888_2.png) [@kittykatattack](https://phaser.discourse.group/u/kittykatattack)\
**Post date:** [September 2, 2020, 3:33pm UTC](https://phaser.discourse.group/t/riding-moving-platforms/7330/3 "2020-09-02T15:33:18Z")

</div>

Thanks @samme!

Hmm… that’s a tricky one! Right, using the collision callback is what I’m looking at now. I think I’ll try offsetting the position of the player by the amount of the platform’s velocity.

(I’ve noticed that the tweened sprites have 0 x/y velocities. Is there a way of obtaining the velocity of tweened sprites, or would I need to calculate that manually?

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [September 2, 2020, 3:50pm UTC](https://phaser.discourse.group/t/riding-moving-platforms/7330/4 "2020-09-02T15:50:12Z")

</div>

Yes, you have to calculate the tweened position delta yourself, perhaps in tween onUpdate.

---

<div class="post-metadata">

**Author:** ![kittykatattack](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/kittykatattack/32/3888_2.png) [@kittykatattack](https://phaser.discourse.group/u/kittykatattack)\
**Post date:** [September 2, 2020, 3:52pm UTC](https://phaser.discourse.group/t/riding-moving-platforms/7330/5 "2020-09-02T15:52:04Z")

</div>

@samme, Thanks! - I will try that!

---

<div class="post-metadata">

**Author:** ![kittykatattack](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/kittykatattack/32/3888_2.png) [@kittykatattack](https://phaser.discourse.group/u/kittykatattack)\
**Post date:** [September 2, 2020, 4:21pm UTC](https://phaser.discourse.group/t/riding-moving-platforms/7330/6 "2020-09-02T16:21:38Z")

</div>

Managed to get it working, thanks to your advice, @samme!

I calculated the platform’s velocity in the tween’s `onUpdate`, like this:

```javascript
    this.tweens.timeline({
              targets: platform,
              ease,
              yoyo,
              loop: -1,
              tweens,
              onUpdate: () => {
                platform.vx = platform.body.position.x - platform.previousX;
                platform.vy = platform.body.position.y - platform.previousY;
                platform.previousX = platform.body.position.x; 
                platform.previousY = platform.body.position.y; 
              }
            }); 

```

Then set up the collision callback to tell the player sprite that the platform collision was occurring and to pass it a reference to the platform:

```javascript
const collisionMovingPlatform = (sprite, platform) => {
      if (platform.body.touching.up && sprite.body.touching.down) {
        sprite.isOnPlatform = true;
        sprite.currentPlatform = platform;      
      }
    };
    //Only allow collisions from top
    const isCollisionFromTop = (sprite, platform) => {
      return platform.body.y > sprite.body.y;
    };

    this.physics.add.collider(
      sprite,
      movingPlatforms,
      collisionMovingPlatform,
      isCollisionFromTop,
      scene
    );

```

Then in the player’s `update` method, applied the platform’s velocity to the player’s position.

```javascript
if (this.isOnPlatform && this.currentPlatform) {
      this.body.position.x += this.currentPlatform.vx;
      this.body.position.y += this.currentPlatform.vy;

      this.isOnPlatform = false;
      this.currentPlatform = null;
    }

```

Works like a charm!

![ezgif-4-fa06f4e080e5](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/7/7c9dffd90f8a8fc2c7bb91ce15c3fc3ff412c624.gif)

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [September 2, 2020, 5:41pm UTC](https://phaser.discourse.group/t/riding-moving-platforms/7330/7 "2020-09-02T17:41:09Z")

</div>

Here is a similar solution, setting a velocity during the tween. It uses [PathFollower#pathDelta](https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.PathFollower.html#pathDelta), which would be much like your `platform.vx, platform.vy`.

https://codepen.io/samme/embed/preview/YzPEqYd?height=300&slug-hash=YzPEqYd&default-tabs=js,result&host=https://codepen.io
