How do you get the velocity of a Kinematic body?

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

First of all by kinematic body I mean the 3D node.
Basically what I am trying to do is make my character’s air speed faster depending on their speed when they leave the ground, which accelerates and so is not constant. How do I get the ‘velocity’ or whatever you would call it of my character and set a variable to it (my character is the kinematic body)? Also the Z axis is locked (like a fighting game) so I only need to set a variable to the X velocity, doubt that makes it easier but that’s what I want specifically. I’m using move_and _slide to move the character, can I retrieve the velocity from that some how?
I know I can just make the air speed faster if they were moving before jumping but I don’t want to do this, because it’s not as smooth.
Thank you in advance

Edit: I’ve realised that since i’m setting the velocity myself I can take it’s current velocity out of my own equations but I would still like to know if I can read it directly from the kinematic mesh.

:bust_in_silhouette: Reply From: Zylann

The point of KinematicBody is that it doesn’t have a velocity of is own, instead it’s up to you to decide what it should be. https://docs.godotengine.org/en/stable/classes/class_kinematicbody.html

Which means velocity is whatever you want. You can animate it, have your own variable storing it, add acceleration values to it over time in _physics_process, reset it, or clamp it when collisions occur etc.
And then, the point is you should use that velocity when using move_and_slide or move_and_collide. If your character already moves, then you have such velocity in some way (otherwise I have no idea how you make it move^^).
Note that if you use move_and_slide, it returns the velocity after sliding detection, which you may use to replace the previous velocity: https://docs.godotengine.org/en/stable/classes/class_kinematicbody.html#class-kinematicbody-method-move-and-slide

About jumping: in your case, when your character jumps, you could change the velocity depending on the velocity from last frame, something like this:

# Call once when the jump button is pressed
func jump():
    # Calculate a 0..1 factor based on known speeds the character can achieve on floor
    var t = inverse_lerp(min_floor_speed, max_floor_speed, velocity.length())
    # Add single jump velocity so the character will shoot upward
    velocity = velocity + Vector2(0, lerp(t, min_jump_speed, max_jump_speed))

You don’t have to do it this way, it’s just an example of how to make jump speed depend on floor speed.

You can follow this tutorial for KinematicBody2D: Using CharacterBody2D/3D — Godot Engine (stable) documentation in English
I know you use 3D, however the concepts are similar, it could help you understand the idea.

Kidscancode also made a video with KinematicBody as part of his 3D series, which includes jumping: https://www.youtube.com/watch?v=ickZ_Genr7A

I did end up taking the velocity I was assigning to it and making that a single variable then using that for my air speed formula. Didn’t even cross my mind that I had assigned it’s velocity myself and so I already know it, which is why I’m going to sleep.

Thanks for all the other info and resources, helped me understand what I’m doing better (I was sort of just plugging code getting results and not fully understanding what it did to get said results).

Kair0ss | 2020-03-05 19:47