0 votes

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 :)
And the community seems amazing!

in Engine by (58 points)

1 Answer

0 votes
Best answer

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.

by (5,192 points)
selected by

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!

velocity.x += (getfloor_velocity().x * delta)

works for me

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.