Vector2 approximately zero with a threshold

Godot Version

v4.4.1 stable

Question

What’s the easiest way to get a Vector2 value around zero within a threshold?

I’m currently using velocity.is_zero_approx()but I’d prefer it to be within like 2 or 3 in either direction e.g (-2 to 2) for both values of the Vector2.

Is there a way to increase the threshold of is_zero_approx? Or would I have to make a custom function which emulations the same functionality?

You could do this instead:

var threshold: float = 2.0
if velocity.length() < threshold:
	#do something
3 Likes

Perfect, exactly what I was after. Thank you kindly! :slight_smile:

1 Like

Depending on your case you also might use length_squared(). length_squared() is more efficient than length() (length_squared() is x * x + y * y whereas length() is Math.sqrt(x * x + y * y) which is one complex operation more).

var threshold_squared: float = 2.0 * 2.0
if velocity.length_squared() < threshold_squared:
    #do something
1 Like