How to make character inherit the velocity of a platform when jumping

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Luka38
:warning: Old Version Published before Godot 3 was released.

Hi everyone, my first post woooo!
Is there any way for a Player, when jumping off a platform, to not fall down immediately, but to actually keep moving at the same velocity that he inherits from the jumping off platform, like in reality.

The Player and the Platform are Kinematic Bodies and I’m using move_and_slide to move the player. I tried using get_collider_velocitybut it only sends the velocity for a split second, then the rest is just (0,0).
Another thing I tried is to use Area2D to get the object that is colliding, and the only thing I can get from the object is it’s position with get_pos.
I know there’s a way to get a velocity from two positions, but i’m pretty new to programing and don’t know how to do it, or is it even worth it if there is a better way to do it.

I’m still new to Godot but it’s a pretty awesome engine so far :slight_smile:
And the community seems amazing!

:bust_in_silhouette: Reply From: avencherus

This may not be the best solution, and I don’ t know much about your implementation to test it. But you should be able to determine velocity by comparing previous position with the current position of an object. It is usually something used in Verlet integration.

So in your platform, assuming it is a Kinematic, you can have some code like this:

extends KinematicBody2D

var prev_pos = get_pos()
var velocity = Vector2()

func _ready():
	set_fixed_process(true)

func _fixed_process(delta):
	velocity = get_pos() - prev_pos
	prev_pos = get_pos()

At which point when you have a reference to it you just check it’s velocity property.

player_velocity += platform.velocity

Big note: You may need to do some additional math with delta, depending on your setup.

Thank you avencherus for a quick answer!
Don’t know how I haven’t thought about it before, but I already had velocity variable of the Platforms, just had to reference it in my Player script and it works.
For anyone in a simmilar problem, here’s a very simplified version, because my script is currently a one big mess :

var platform
var platform_velocity
var timer
var is_in_air # When player is not colliding with anything

func _ready():
    #(some timer code)

func _fixed_process(delta):
    # Your other movment code
    var  time_left = timer.get_time_left()
    platform_velocity = platform.velocity

    if is_in_air:
      velocity.x = speed.x + (platform_velocity.x / delta) * time_left)
    else:
      velocit.x = speed.x

    velocity = move_and_slide(velocity, FLOOR_NORMAL, SLOPE_FRICTION)

func _on_Area2D_body_enter( body ):
     platform = body

Thank you avencherus for the help!

Luka38 | 2017-10-12 10:00

_velocity.x += (get_floor_velocity().x * delta)

works for me

ChromiumOS | 2021-01-24 13:50