How to move KinematicBody2D with a platform?

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

I have a player, which is a KinematicBody2D, and I have a moving platform (which is also a KinematicBody2D) which moves up and down like a sine wave (but only moves upwards and downwards, not to the sides), and I want that when the player steps in the platform, it will stand in top of it (like if it was stuck to the platform). I found some places where this is asked, but it doesn’t seem to work in Godot 3.

If I just try it out, the player stands in the platform, the platform goes down, when it tries to go up it can’t because of the player, and continues then to go down.

I added some code that looked like:

if collider.moving: #moving is boolean variable set to true in this case
    speed = collider.speed

But ths has some sort of jitter still.
(btw I move the bodies with move_and_slide(speed)

Can anyone help me please?

Thanks in advance.

What happens if you move the body manually, like described here under the simulated motion section? link

literalcitrus | 2018-02-02 04:08

I don’t get your point, I am already moving the body manually using the move_and_slide (speed) in the physics_process ().
Could you please better explain what you meant?

PugMasterPug | 2018-02-02 04:38

The move_and_slide() function is part of the API that moves kinematic bodies in a way that allows you to interact with collisions in a friendly way (internally it uses what used to be move() but with some additional slide calculations). By manually, I mean through modifying or animating the position property directly. That’s what the “Simulated Movement” in that article is referring to.

literalcitrus | 2018-02-02 04:59

(sorry for answering 10 hours late, my timezone is probably diferent than yours).
If I move the player using position += speed*delta, the player just falls through everything, so I think you were saying that I should move the platform like that, so when I do, everything works out fine, but when the platform goes down, the player a few pixels on top of the platform, and when the platform rises, the player gets a little bit inside the platform. I am trying to correct that small problem, but I haven’t been able to.

PugMasterPug | 2018-02-02 15:38

:bust_in_silhouette: Reply From: gtkampos

You must use the code:

velocity = get_floor_velocity()

See the docs: KinematicBody2D — Godot Engine (3.0) documentation in English

Stil doesn’t work.
If I set speed=get_floor_velocity () constantly, then when the floor is going down, the player just floats in the air.
If I say speed += get_floor_velocity () I get the same problem as before.
If I set speed = get_floor_velocity () only when it is colliding with the platform, I get the same problem as before.
If I set speed += get_floor_velocity () only when it is colliding, I get the same problem as before.
(when I refer to tge same problem as before, I mean that when the platform is supposed to rise, it stays still due to the player)

PugMasterPug | 2018-02-02 15:30

Can you post your code?

gtkampos | 2018-02-02 18:25

here it is.

PugMasterPug | 2018-02-02 20:21

Not time to see all your code, but lets try something first:

If you are using Godot 3.0, remove the _ready() function from your code, in 3.0 you dont need use anymore _set_physics_proccess(true). It will run if it is in the code.

speed += get_floor_velocity() must be before the move_and_slide function and you could disable gravity code, since it could make you jitter on the platform if moving up or down.

If not work, I will try to make it work in home and post here.

gtkampos | 2018-02-08 14:36