How to tell if an object is moving or getting its speed / velocity

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

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 ??

:bust_in_silhouette: Reply From: 2D

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)

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 move_and_slide etc). So… you already know when it moves.

kidscancode | 2019-12-31 17:34

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.

2D||!2D | 2019-12-31 21:23

:bust_in_silhouette: Reply From: kidscancode

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.