Moving Platforms 'Sync to Physics' problem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JayH


I am making a 2D pixel platformer which has moving platforms.

My platforms and player are Kinematic bodies. The Player moves with move_and_slide and the platforms move with a tween.

I read that in 3.1 there was a new feature Snap to physics, an option in the Inspector for Kinematic bodies. This is meant to sync the players movement to the moving platform, at least that’s what I understood.

I intended to use this for fixing the problem where the player gets nudged horizontally as the horizontal moving platform changes direction: If I’m doing this correctly, I enabled ‘Snap to physics’ on the moving platforms Kinemetic body (tick box).

But when I enable this, the CollisonShape2D of the moving platform no longer moves along with the moving platform :frowning: this leaves the player standing in mid-air on the now static collisionshape2d.

I also read somewhere else that possibly you can’t use move_and_slide with ‘Snap to physics’. Is this true?

What method should I be using to move players and moving platforms then if I wish to avail of the synced physics for moving platforms?

I’m a little confused.


:bust_in_silhouette: Reply From: max.marauder

sync_to_physics should be set to true for Kinematic bodies that are intended to be moved by manipulating their positions (e.g. setting a position directly, or by using Tween or AnimationPlayer, etc.).
And it should stay false for bodies that are moved by the physics engine as a result of calling move_and_collide, move_and_slide, or move_and_slide_with_snap.

If this is how you set it, then the problem is somewhere else.

I had a behavior similar to what you described when my moving platform scene was not of KinematicBody2D type itself, but contained KinematicBody2D as one of the children nodes.
For some reason in such case the KinematicBody2D position is not updating automatically on moving the parent node (probably an engine bug), and I had to update it manually by setting to Vector2.ZERO in _physics_process.
Can it be also your case?

Thanks for the reply.

With the Collisionshape2D not moving along with the sprite I think it must be a bug as I created a different project with animated frames for the platform to test and it happened once on that, but I couldn’t re-create.

I’ve since found a method that works better with code, following this tutorial:

When I checked the ‘Sync to Physics’ in the Inspector, everything worked fine and I also didn’t need to do anything else for the player to allow him to stick to the platforms, whether horizontally or vertically - it just worked :wink:

JayH | 2022-06-23 17:56