0 votes

code error!

Normally this code works fine.
After a few times it will be stuck because linearvelocity.y == 0 never returns True but the result of linearvelocity.y is always logged out to 0.

in Engine by (15 points)

1 Answer

+2 votes
Best answer

You should never compare floating point values via ==. That's because minor floating point errors will cause the equality to fail sometimes - just as you're experiencing.

Based on what you're doing, you typically want to verify that some value is "sufficiently close" to some other value.

In your case, you could do something like:

if abs(linear_velocity.y) < 0.001:

... or something similar...

by (10,914 points)
selected by

is_equal_approx() and Vector2.is_equal_approx() were created for just this situation.

good tip - thanks.

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.