0 votes

I have a kinematicbody 2D object, I am wondering if there is a way I can tell if it moving or maybe getting its moving velocity ?

The only way I can think of is by using 2 vector2 variable which records its position on this frame and previous frame. if it is not the same , then it is moving otherwise it is not.
Is there a better / simpler way ??

asked Dec 31, 2019 in Engine by lowpolygon (164 points)

2 Answers

0 votes

I think the two Vector2 is the best way to do it actually. I did some looking and found this post where Zylann confirmed our thoughts. I recommend using a threshold like the following:

if previous_frame_pos.distance_squared_to(current_frame_pos) >= 0.01:
    #Character is moving
    print(previous_frame_pos, " : ", current_frame_pos)
answered Dec 31, 2019 by 2D||!2D (383 points)

His answer doesn't say that at all. It says

KinematicBody doesn't move on its own, it expects YOU to move it using a script (using moveandslide etc). So... you already know when it moves.

Correct, kinematic bodies don't move on their own, but in the second part of his answer,
I believe it supports doing what I said. The move functions just tell it to move, but you don't know if it actually moved unless you examine the return values of the function. And determing that can be more effort than just looking at a before and after position.

0 votes

You have to know a kinematic body's velocity already if you're moving it in the first place. A kinematic can only move when you apply a velocity in one the move_and_* methods.

answered Dec 31, 2019 by kidscancode (17,714 points)
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.