Stopping 3D KinematicBody on exact location

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By echinococcus
:warning: Old Version Published before Godot 3 was released.

Hey guys I need a bit of help, I’m creating RTS like game and I’m stuck with KinematicBody movement.

When I click the right mouse button the code does a ray cast and gets the location on the ground where the body should move. Now I get the direction vector by doing:

(player_position - location).normalized()

The body moves like it’s supposed too, I’ve limited the movement on Z and X axis.

Now I can’t get the body to stop on exact location:

I tried to do something like this:

if (player_position == location):

Basically comparing Vector3s, but they never match exactly, since I’m using the move function to move the KinematicBody.
Do you have any idea how I can stop the body on that location on the ground, I was thinking of creating an offset or something, but I can’t really get it to work.
Any help will be appreciated.

:bust_in_silhouette: Reply From: mollusca

You can check the distance to the target position:

if player_position.distance_squared_to(location) <= 0.1:

However if your unit is moving fast it might overshoot and never get close enough to stop. You can make the stopping limit larger or you can slow the unit down as it gets closer:

var dist = player_position.distance_to(location)
if dist <= 0.1:
    var dir = (player_position - location).normalized()
    #the min makes sure the unit never goes faster than MAX_VEL
    var vel = dir * MAX_VEL * min(1.0, dist / 100)
    #rest of movement code

Adjust the constants to your liking.