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.

asked May 8 in Engine by Dat Vu (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...

answered May 8 by jgodfrey (5,246 points)
selected May 9 by Dat Vu

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.