0 votes

Hi guys, I have a problem with my KinematicBody2D
It is supossed to be a ship that goes to a certain point:

Vector2 motion = (point - global_position)
move_and_slide(motion * speed)
if (global_position.distance_to(point) == 0):
do_stuff()

This is called in process, and the dostuff() method is not getting called. I suposse this is because of the stuttering that the body has when it reaches the point vector
Anyone has an idea of what can I do to fix this? Thanks

in Engine by (97 points)

2 Answers

0 votes
Best answer

I had to implement my own tolerance method, because isequalapprox() didn't work for me. This is a snippet of what I did:

if distance >= (stop_distance - tolerance) and distance <= (stop_distance + tolerance):
do_stuff()
by (97 points)
+2 votes

You should never compare a floating point value to another value using ==. There are likely very minor floating point differences between the numbers that will prevent them from being exactly equal. Instead, you need to compare that they are within some small tolerance of being the same.

You can either do that yourself or use the in-built:

is_equal_approx ( float a, float b )

So, in your case:

if is_equal_approx(global_position.distance_to(point), 0):
by (12,290 points)
edited by
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.