Hello again

could anyone advise how i can check if a Vector2 is zero i.e not moving?

would also help if someone could advise the code to check if input not pressed i.e. any key or action not pressed.

Thanks

Hey

@Sparrow said: could anyone advise how i can check if a Vector2 is zero i.e not moving? Best start to get help is the documentatoin. Take a look here: https://docs.godotengine.org/en/3.0/classes/class_vector2.html There you see that Vector2 has the method length which returns the length of the vector. As a gamedeveloper it is important to know some vector math. Vectors arn't that hard. For example, you can check wikipedia to see what the length of a vector actually is. For example, the length for a two dimensional vector is calculated as: squareroot of (x x + y y). That said, if x and y are zero, the length of the vector is zero. Makes sense, doesn't it? If you don't want to use the length method, you can just check if x and y are zero.

var zero_vector = Vector2(0, 0)
var not_zero_vector = Vector2(42, 0)

if zero_vector.length() == 0:
	print("zero_vector has a length of 0")
else:
	print("zero_vector has a length unequal to 0")

if zero_vector.x == 0 && zero_vector.y == 0:
	print("both components of zero_vector are 0")
else:
	print("not both components of zero_vector are 0")

if not_zero_vector.length() == 0:
	print("zero_vector has a length of 0")
else:
	print("not_zero_vector has a length unequal to 0")

if not_zero_vector.x == 0 && not_zero_vector.y == 0:
	print("both components of not_zero_vector are 0")
else:
	print("not both components of not_zero_vector are 0")

print("the length of zero_vector is %f" % zero_vector.length())
print("the length of not_zero_vector is %f" % not_zero_vector.length())

@Sparrow said: would also help if someone could advise the code to check if input not pressed i.e. any key or action not pressed.

You can check if an input action is "pressed" with:

if Input.is_action_pressed("ui_right"):
	print("ui_right is pressed")

You can reverse a boolean value with the not operator, which is the '!' sign. Take a look:

if !Input.is_action_pressed("ui_right"):
	print("ui_right is not pressed")

Take a look at this for inputs.

@Schorsch said: Hey

@Sparrow said: could anyone advise how i can check if a Vector2 is zero i.e not moving? Best start to get help is the documentatoin. Take a look here: https://docs.godotengine.org/en/3.0/classes/class_vector2.html There you see that Vector2 has the method length which returns the length of the vector. As a gamedeveloper it is important to know some vector math. Vectors arn't that hard. For example, you can check wikipedia to see what the length of a vector actually is. For example, the length for a two dimensional vector is calculated as: squareroot of (x x + y y). That said, if x and y are zero, the length of the vector is zero. Makes sense, doesn't it? If you don't want to use the length method, you can just check if x and y are zero.

var zero_vector = Vector2(0, 0)
var not_zero_vector = Vector2(42, 0)

if zero_vector.length() == 0:
	print("zero_vector has a length of 0")
else:
	print("zero_vector has a length unequal to 0")

if zero_vector.x == 0 && zero_vector.y == 0:
	print("both components of zero_vector are 0")
else:
	print("not both components of zero_vector are 0")

if not_zero_vector.length() == 0:
	print("zero_vector has a length of 0")
else:
	print("not_zero_vector has a length unequal to 0")

if not_zero_vector.x == 0 && not_zero_vector.y == 0:
	print("both components of not_zero_vector are 0")
else:
	print("not both components of not_zero_vector are 0")

print("the length of zero_vector is %f" % zero_vector.length())
print("the length of not_zero_vector is %f" % not_zero_vector.length())

@Sparrow said: would also help if someone could advise the code to check if input not pressed i.e. any key or action not pressed.

You can check if an input action is "pressed" with:

if Input.is_action_pressed("ui_right"):
	print("ui_right is pressed")

You can reverse a boolean value with the not operator, which is the '!' sign. Take a look:

if !Input.is_action_pressed("ui_right"):
	print("ui_right is not pressed")

Take a look at this for inputs.

works great the Length is what i need. the ! not function is good too. thanks

4 years later