+1 vote

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.

in Engine by (15 points)
edited by

1 Answer

+2 votes

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: https://docs.godotengine.org/en/stable/tutorials/physics/using_kinematic_body_2d.html
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

by (27,962 points)
edited by

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).

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.