2D Movement Error

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

My code says the following:

    const maxval = 300
var velocity = Vector2.ZERO

func _physics_process(delta):
	var input = Vector2.ZERO
	if Input.is_action_pressed("ui_right"):
		input.x = 1
	elif Input.is_action_pressed("ui_left"):
		input.x = -1
	if Input.is_action_pressed("ui_up"):
		input.y = 1
	elif Input.is_action_pressed("ui_down"):
		input.y = -1
		input = 0
	if input != Vector2.ZERO:
		velocity = input
		velocity = Vector2.ZERO
# warning-ignore:return_value_discarded
	move_and_collide(velocity * delta * maxval)

But I’m getting this error:

  Invalid operands 'int' and 'Vector2' in operator '!='.

I have animations for movement but no Idle animations. And when I just use another code from another project, the animation either plays and doesn’t stop or never plays.

:bust_in_silhouette: Reply From: kidscancode

You declared your input variable as a Vector2, and when there’s input you change its x and y components. However, anytime you don’t press “up” or “down” you set input = 0, which is an int. That is the source of your error: != can’t compare an int to a vector.