0 votes

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):
     #stop

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.
Thanks!

in Engine by (14 points)

1 Answer

0 votes

You can check the distance to the target position:

if player_position.distance_squared_to(location) <= 0.1:
    #stop

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:
    #stop
else:
    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.

by (1,562 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 Frequently asked questions and 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 [email protected] with your username.