Using a Vector3 as a boolean in an if?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nanto2016

In the CharacterBody3D script template, inside _physics_process() there is this code:

var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
	velocity.x = direction.x * SPEED
	velocity.z = direction.z * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	velocity.z = move_toward(velocity.z, 0, SPEED)

move_and_slide()

I do not understand what the if direction means… direction is a Vector3 so how can i beused as a boolean inside if?

:bust_in_silhouette: Reply From: Bypell

“In a boolean context, a Vector3 will evaluate to false if it’s equal to Vector3(0, 0, 0). Otherwise, a Vector3 will always evaluate to true.”

In other words, if direction’s length is higher than zero, the player starts moving in that direction. If not, the player stops moving.